[英]Regex matching character preceded by a pattern
Given the following setting:鉴于以下设置:
input = "window.addEventListener('click', (s, d) => {});"
input =
"window.addEventListener('click', (s, d) => {});"
desired match = "(s, d) =>"
所需的匹配=
"(s, d) =>"
tried the regex尝试了正则表达式
(.*).*=>
But this will match "('click', (s, d) =>"
, which is not desired.但这将匹配
"('click', (s, d) =>"
,这是不希望的。
Any ideas on how to modify the regex so that the match only starts from the closest opening parentheses before the "=>"
symbol?关于如何修改正则表达式以便匹配仅从
"=>"
符号之前最接近的左括号开始的任何想法?
Any help is much appreciated!任何帮助深表感谢!
You may use this regex with re.findall
:您可以将此正则表达式与
re.findall
一起re.findall
:
>>> s = "window.addEventListener('click', (s, d) => {});"
>>> print (re.findall(r'\([^()]*\)\s*=>', s)[0])
(s, d) =>
RegEx Details:正则表达式详情:
\\(
: Match (
\\(
: 匹配(
[^()]*
: Match 0 or more of any character that is not (
and )
[^()]*
: 匹配 0 个或多个不是(
和)
的任何字符\\)
: Match )
\\)
: 匹配)
\\s*=>
: Match 0 or more whitespaces followed by text =>
\\s*=>
:匹配 0 个或多个空格后跟文本=>
this is what you need to use.这是你需要使用的。 lookbehind more precisely https://javascript.info/regexp-lookahead-lookbehind
更准确地回顾https://javascript.info/regexp-lookahead-lookbehind
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.