将正则匹配在带有正面的字符串之后,并在每个选择后面输入它

发布于 2025-02-01 09:11:47 字数 551 浏览 2 评论 0原文

版权所有:Hololive Hololive_english
角色:mori_calliope takanashi_kiara takanashi_kiara_(凤凰)
艺术家:Xu_chin-wen
物种:
元:Web

我想在EG之后选择每个单词:字符:,以便我可以将例如:字符:在每个选择后面

字符: mori_calliope 字符: takanashi_kiara chararcter: takanashi_kiara_(phoenix)

我得到的最接近的东西是,我得到

(?<=(\w*):\s*\S*\s.*)(?<=\s)(?=\S)

的是正常工作的,但是当单身时它会破裂例如:字符:某物或当它空的时,

如果有人会帮助我,我真的很感激

copyright: hololive hololive_english
character: mori_calliope takanashi_kiara takanashi_kiara_(phoenix)
artist: xu_chin-wen
species:
meta: web

I want to select every word after eg:character: so i can put eg:character: behind every selection,

character:mori_calliope character:takanashi_kiara chararcter:takanashi_kiara_(phoenix)

the closest thing i got is

(?<=(\w*):\s*\S*\s.*)(?<=\s)(?=\S)

which works properly but it breaks when there is a single entry on eg:character: something or when its empty

i would be really thankfull if someone would help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七婞 2025-02-08 09:11:47

您应该安装 pypi regex module 并使用

regex.sub(r'(?<=(\w+):.*)(?<=\s)(?=\S)', r'\1:', text)
# or
# regex.sub(r'(?<=(\w+:).*)(?<=\s)(?=\S)', r'\1', text)

请参见 Regex Demo
详细信息

  • (?其次是 char,然后除了尽可能多的挂断字符以外的任何零或更多字符
  • ( 与非白人char。

See the Python demo

import regex
text = "copyright: hololive hololive_english\ncharacter: mori_calliope takanashi_kiara takanashi_kiara_(phoenix)\nartist: xu_chin-wen\nspecies:\nmeta: web"
print( regex.sub(r'(?<=(\w+):.*)(?<=\s)(?=\S)', r'\1:', text) )

输出:

copyright: copyright:hololive copyright:hololive_english
character: character:mori_calliope character:takanashi_kiara character:takanashi_kiara_(phoenix)
artist: artist:xu_chin-wen
species:
meta: meta:web

You should install PyPi regex module and use

regex.sub(r'(?<=(\w+):.*)(?<=\s)(?=\S)', r'\1:', text)
# or
# regex.sub(r'(?<=(\w+:).*)(?<=\s)(?=\S)', r'\1', text)

See the regex demo.
Details:

  • (?<=(\w+):.*) - a positive lookbehind that matches a location that is immediately preceded with any word (captured into Group 1) followed by a : char and then any zero or more chars other than line break chars as many as possible
    (?<=\s)` - a positive lookbehind that matches a location that is immediately preceded with a whitespace char
  • (?=\S) - a positive lookahead that matches a location that is immediately followed with a non-whitespace char.

See the Python demo:

import regex
text = "copyright: hololive hololive_english\ncharacter: mori_calliope takanashi_kiara takanashi_kiara_(phoenix)\nartist: xu_chin-wen\nspecies:\nmeta: web"
print( regex.sub(r'(?<=(\w+):.*)(?<=\s)(?=\S)', r'\1:', text) )

Output:

copyright: copyright:hololive copyright:hololive_english
character: character:mori_calliope character:takanashi_kiara character:takanashi_kiara_(phoenix)
artist: artist:xu_chin-wen
species:
meta: meta:web
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文