2024-04-24 09:38:13 发布
网友
我在python中有以下字符串:
text = "vagrant 11450 4344 0 Feb22 pts/2 00:00:28 python run.py abc"
我想捕获时间字段后的文本,即“python”运行.py“abc”
我正在使用下面的正则表达式,但它不起作用
[\d:]+ (.)*
你可以用
\d+:\d+\s+(.*)
参见regex demo。你知道吗
细节
\d+
:
\s+
(.*)
.group(1)
参见Python demo:
import re text = "vagrant 11450 4344 0 Feb22 pts/2 00:00:28 python run.py abc" m = re.search(r'\d+:\d+\s+(.*)', text) if m: print(m.group(1)) # => python run.py abc
无RE:
text = "vagrant 11450 4344 0 Feb22 pts/2 00:00:28 python run.py abc" text=text.split(":")[-1][3:]
输出:
python run.py abc
使用re.search()函数:
re.search()
import re text = "vagrant 11450 4344 0 Feb22 pts/2 00:00:28 python run.py abc" result = re.search(r'(?<=(\d{2}:){2}\d{2} ).*', text).group() print(result)
你可以用
参见regex demo。你知道吗
细节
\d+
-1个或更多数字:
-冒号\d+
-1个或更多数字\s+
-1个或多个空格字符(.*)
-group1(您需要使用.group(1)
访问的值):除换行符以外的任何0+字符,尽可能多(行的所有其余部分)。你知道吗参见Python demo:
无RE:
输出:
使用
re.search()
函数:输出:
相关问题 更多 >
编程相关推荐