用python正则表达式,根据关键字 [ ] wf_data: 获取字符串“[21:35:28.480]xxxxxxwf_data: 1234\n[21:35:28.480]xxxxxxwf_data: 1234\n”里面的21 35 28 480 1234
时间: 2023-04-08 15:01:46 浏览: 61
可以使用以下代码来实现:
import re
string = "[21:35:28.480]xxxxxxwf_data: 1234\n[21:35:28.480]xxxxxxwf_data: 1234\n"
pattern = r"\[(\d{2}):(\d{2}):(\d{2})\.(\d{3})\]xxxxxxwf_data: (\d+)\n"
matches = re.findall(pattern, string)
for match in matches:
print(match[0], match[1], match[2], match[3], match[4])
输出结果为:
21 35 28 480 1234
21 35 28 480 1234
这里使用了正则表达式的分组功能,将需要提取的部分用括号括起来,然后在匹配时使用 findall 函数,将所有匹配结果以元组的形式返回。最后遍历所有匹配结果,取出需要的部分即可。
相关问题
用python正则表达式,根据关键字 [ ] wf_data: 获取字符串“[21:35:28.480]xxxxxxwf_data: 1234”里面的21 35 28 480 1234
可以使用以下代码实现:
import re
text = "[21:35:28.480]xxxxxxwf_data: 1234"
pattern = r"\[(\d{2}):(\d{2}):(\d{2})\.(\d{3})\].*wf_data: (\d+)"
match = re.search(pattern, text)
if match:
hour = match.group(1)
minute = match.group(2)
second = match.group(3)
millisecond = match.group(4)
data = match.group(5)
print(hour, minute, second, millisecond, data)
else:
print("No match")
用python正则表达式获取字符串“[21:35:28.480]wf_data: 1234”里面的21 35 28 480 1234
可以使用以下代码来获取字符串中的数字和时间:
import re
string = "[21:35:28.480]wf_data: 1234"
pattern = r"\[(\d+):(\d+):(\d+)\.(\d+)\]wf_data: (\d+)"
match = re.match(pattern, string)
if match:
hour = match.group(1)
minute = match.group(2)
second = match.group(3)
millisecond = match.group(4)
number = match.group(5)
print(hour, minute, second, millisecond, number)
else:
print("No match found")