簡體   English   中英

使用 Regex Python 搜索括號內的所有值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM