[英]Search all values within parenthesis by using Regex Python
我有一个类似这种格式的文本列表-
2021-07-11 18:34:24,381:鼠标点击 (159, 88) 与 Button.left
在这里,我想用整个文本文件中的括号搜索这个“(159, 88)”模式。
括号中的第 1 和第 2 值可以是一位到四位。 我的意思是这种格式可以是 (1, 888) 或 (20, 32) 或 (134, 4) 或 (365, 567) 或 (1240, 122) 或 (1345, 1245)。
现在,通过使用正则表达式,我该如何解决这个问题?
我的预期输出是 - (384, 567)
请帮忙。
使用模式'\\(\\d+,\\s*\\d+\\)'
来匹配您想要的子字符串。
>>> import re
>>> text = '2021-07-11 18:34:24,381: Mouse clicked at (159, 88) with Button.left'
>>> re.findall('\(\d+,\s*\d+\)', text)
['(159, 88)']
理解模式: '\\(\\d+,\\s*\\d+\\)'
\( : Look for opening parenthesis, \ is used as escape character
\d+ : Look for one or more digits
, : Look for exactly one comma
\s* : Zero or more white space characters
\d+ : Look for one or more digits again
\) : Look for closing parenthesis, \ is used as escape character
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.