我必须在包含特殊字符(^)的单词之后提取一行的所有单词。从以下几行:
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
提前谢谢。
答案 0 :(得分:0)
您可以使用
re.findall(r'\^\S*\s*(.*)', s)
或者,仅获得第一场比赛:
m = re.search(r'\^\S*\s*(.*)', s)
if m:
print(m.group(1))
请参阅regex demo。
<强>详情
\^
- 文字^
匹配\S*
- 0+非空白字符\s*
- 然后是0 +空白字符(.*)
- 捕获第1组:除换行符(LF)以外的任何0或更多字符。请参阅Python demo:
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
答案 1 :(得分:0)