[英]How to match negative lookbehind with unknown characters between?
I need to match all .get('asfd')
, but only in the case where .wait(.*)
doesn't exist beforehand. 我需要匹配所有
.get('asfd')
,但是仅在事先不存在.wait(.*)
的情况下。
.wait(500).get('asdf') // shouldn't match
.asdf('asdf').get('asdf') // should match
Unfortunately, negative look-behinds don't support quantifiers, so I'm not sure how to describe the void between .wait(
and ).get('asdf')
for \\d*
不幸的是,负向后看不支持量词,因此我不确定如何描述
\\d*
.wait(
和).get('asdf')
之间的空白。
What's the approach for matching this unquantifiable area? 匹配此不可量化区域的方法是什么?
I figure I need some way to describe that there wasn't a wait
behind the last set of parenthesis, but is there a simple way to do that? 我想我需要某种方式来描述在最后一组括号后面没有
wait
,但是有一种简单的方法可以做到这一点吗?
Thanks 谢谢
Ok, it took quite a lot of experimenting, and asking this question helped to clarify the situation. 好的,花了很多时间进行实验,提出这个问题有助于弄清情况。
The answer is to describe the in-between: separate from the look-behind . 答案是要描述两者之间:与后向分离 。
(?<!wait)
(?:\([^)]*\))
(\.get\(.*\))
That second section allows any character until a parenthesis. 第二部分允许任何字符,直到有括号为止。 Sometimes, the first parenthesis appears inside quotes, and should be ignored.
有时,第一个括号出现在引号内,应该忽略。 Not accounting for escaped quotes, my entire regex became:
不考虑转义的引号,我的整个正则表达式变为:
((?<!wait)\(.*\)\s*)(\.get\((?:"[^"]*"|'[^']*')[^\)]*\))
And I use it to insert .wait()
before/after .get()
with match groups 1 ( $1
) and 2 ( $2
) 我用它在匹配组1(
$1
)和2( $2
)的.get()
.wait()
之前/之后插入.wait()
)
$1.wait(234)$2.wait(234)
I am not a regex expert, but how about this? 我不是正则表达式专家,但是如何呢?
/^(?!\\.wait\\(\\d+\\)).*\\.get\\(.*\\)/g
Explanation:
说明:
(?!
Negative lookahead. Specifies a group that can not match after the main expression (if it matches, the result is discarded).
(?!
负向超前。指定在主表达式后不能匹配的组(如果匹配,则结果将被丢弃)。
\\.
Escaped character.
转义字符。 Matches a "."
匹配“。” character (char code 46).
字符(字符代码46)。
w
Character.
w
角色。 Matches a "w" character (char code 119). 匹配“ w”字符(字符代码119)。 Case sensitive.
区分大小写。
a
Character.
a
角色。 Matches a "a" character (char code 97). 匹配一个“ a”字符(字符代码97)。 Case sensitive.
区分大小写。
i
Character.
i
性格。 Matches a "i" character (char code 105). 匹配“ i”字符(字符代码105)。 Case sensitive.
区分大小写。
t
Character.
t
字符。 Matches a "t" character (char code 116). 匹配一个“ t”字符(字符代码116)。 Case sensitive.
区分大小写。
\\(
Escaped character. Matches a "(" character (char code 40).
\\(
转义字符。匹配一个“(”字符(字符代码40)。
\\d
Digit.
\\d
数字。 Matches any digit character (0-9). 匹配任何数字字符(0-9)。
+
Quantifier.
+
量词。 Match 1 or more of the preceding token. 匹配1个或多个前面的令牌。
\\)
Escaped character.
\\)
转义字符。 Matches a ")" character (char code 41). 匹配“)”字符(字符代码41)。
.
Dot.
点。 Matches any character except line breaks.
匹配除换行符以外的任何字符。
*
Quantifier.
*
量词。 Match 0 or more of the preceding token. 匹配0个或多个前面的令牌。
\\.
Escaped character.
转义字符。 Matches a "."
匹配“。” character (char code 46).
字符(字符代码46)。
g
Character.
g
字符。 Matches a "g" character (char code 103). 匹配“ g”字符(字符代码103)。 Case sensitive.
区分大小写。
e
Character.
e
字符。 Matches a "e" character (char code 101). 匹配“ e”字符(字符代码101)。 Case sensitive.
区分大小写。
t
Character.
t
字符。 Matches a "t" character (char code 116). 匹配一个“ t”字符(字符代码116)。 Case sensitive.
区分大小写。
\\(
Escaped character. Matches a "(" character (char code 40).
\\(
转义字符。匹配一个“(”字符(字符代码40)。
.
Dot.
点。 Matches any character except line breaks.
匹配除换行符以外的任何字符。
*
Quantifier.
*
量词。 Match 0 or more of the preceding token. 匹配0个或多个前面的令牌。
\\)
Escaped character.
\\)
转义字符。 Matches a ")" character (char code 41). 匹配“)”字符(字符代码41)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.