简体   繁体   中英

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.

.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*

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?

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.wait(234)$2.wait(234)

在此处输入图片说明

在此处输入图片说明

I am not a regex expert, but how about this?

/^(?!\\.wait\\(\\d+\\)).*\\.get\\(.*\\)/g

(?! Specifies a group that can not match after the main expression (if it matches, the result is discarded). 指定在主表达式后不能匹配的组(如果匹配,则结果将被丢弃)。

\\. Matches a "." character (char code 46).

w Matches a "w" character (char code 119). Case sensitive.

a Matches a "a" character (char code 97). Case sensitive.

i Matches a "i" character (char code 105). Case sensitive.

t Matches a "t" character (char code 116). Case sensitive.

\\( Matches a "(" character (char code 40). 匹配一个“(”字符(字符代码40)。

\\d Matches any digit character (0-9).

+ Match 1 or more of the preceding token.

\\) Matches a ")" character (char code 41).

. Matches any character except line breaks.

* Match 0 or more of the preceding token.

\\. Matches a "." character (char code 46).

g Matches a "g" character (char code 103). Case sensitive.

e Matches a "e" character (char code 101). Case sensitive.

t Matches a "t" character (char code 116). Case sensitive.

\\( Matches a "(" character (char code 40). 匹配一个“(”字符(字符代码40)。

. Matches any character except line breaks.

* Match 0 or more of the preceding token.

\\) Matches a ")" character (char code 41).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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