我必须在包含特殊字符(^)的单词之后提取一行中的所有单词。从以下几行:
skipping root#14^wvoice as file not exists
node wroot#15^GetBuildType already complete
预期输出:
as file does not exists
already complete
我尝试使用正则表达式如下:
(?<=\^\w).*
但得到输出为:
wvoice as file not exists
GetBuildType already complete
先感谢您。
您可以使用
re.findall(r'\^\S*\s*(.*)', s)
或者,只获得第一场比赛:
m = re.search(r'\^\S*\s*(.*)', s)
if m:
print(m.group(1))
请参阅正则表达式演示。
细节
\^
- 文字^
匹配\S*
- 0+ 非空白字符\s*
- 然后是 0+ 个空白字符(.*)
- 捕获组 1:除换行符 (LF) 之外的任何 0 个或多个字符。请参阅Python 演示:
import re
texts=['skipping root#14^wvoice as file not exists','node wroot#15^GetBuildType already complete']
for text in texts:
m=re.search(r'\^\S*\s*(.*)', text)
if m:
print(m.group(1))
输出:
as file not exists
already complete
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句