[英]How to parse this string in python using regex?
I have the following string in python: 我在python中有以下字符串:
text = "vagrant 11450 4344 0 Feb22 pts/2 00:00:28 python run.py abc"
I want to capture the text after time field that is "python run.py abc" 我想在时间字段之后捕获文本,即“ python run.py abc”
I am using the following regex but it is not working 我正在使用以下正则表达式,但无法正常工作
[\d:]+ (.)*
You may use 您可以使用
\d+:\d+\s+(.*)
See the regex demo . 参见regex演示 。
Details 细节
\\d+
- 1 or more digits \\d+
-1个或更多数字 :
- a colon :
-冒号 \\d+
- 1 or more digits \\d+
-1个或更多数字 \\s+
- 1 or more whitespace chars \\s+
-1个或多个空格字符 (.*)
- Group 1 (the value you need to access using .group(1)
): any 0+ chars other than line break chars, as many as possible (all the rest of the line). (.*)
-组1(您需要使用.group(1)
访问的值):除换行符以外的任何0+字符都应尽可能多(其余所有行)。 See the Python demo : 参见Python演示 :
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
With re.search()
function: 使用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)
The output: 输出:
python run.py abc
Without RE: 没有RE:
text = "vagrant 11450 4344 0 Feb22 pts/2 00:00:28 python run.py abc"
text=text.split(":")[-1][3:]
Output: 输出:
python run.py abc
You can use re.split
and regex :\\d{2}:\\d{2}\\s+
. 您可以使用re.split
和regex :\\d{2}:\\d{2}\\s+
。
text = 'vagrant 11450 4344 0 Feb22 pts/2 00:00:28 python run.py abc'
str = re.split(r':\d{2}:\d{2}\s+', text)[1]
Output: python run.py abc
输出: python run.py abc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.