简体   繁体   English

使用 Regex Python 搜索括号内的所有值

[英]Search all values within parenthesis by using Regex Python

I have a list of text like this format-我有一个类似这种格式的文本列表-

2021-07-11 18:34:24,381: Mouse clicked at (159, 88) with Button.left 2021-07-11 18:34:24,381:鼠标点击 (159, 88) 与 Button.left

Here, I want to search this "(159, 88)" pattern with the bracket from my entire text file.在这里,我想用整个文本文件中的括号搜索这个“(159, 88)”模式。

The 1st and 2nd values in the parenthesis can be One digit to four digits.括号中的第 1 和第 2 值可以是一位到四位。 I mean this format can be (1, 888) or (20, 32) or (134, 4) or (365, 567) or (1240, 122) or (1345, 1245).我的意思是这种格式可以是 (1, 888) 或 (20, 32) 或 (134, 4) 或 (365, 567) 或 (1240, 122) 或 (1345, 1245)。

Now, by using regex, how do I solve this problem?现在,通过使用正则表达式,我该如何解决这个问题?

My expected output is- (384, 567)我的预期输出是 - (384, 567)

Please help.请帮忙。

Use the pattern '\\(\\d+,\\s*\\d+\\)' to match the substring you want.使用模式'\\(\\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)']

Understanding the pattern: '\\(\\d+,\\s*\\d+\\)'理解模式: '\\(\\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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM