简体   繁体   English

如何将负向后搜索与未知字符之间进行匹配?

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

相关问题 否定后视不匹配转义字符,在转义反斜杠上失败 - Negative lookbehind to not match escaped characters, fails on escaped backslash 负向后看:匹配未在一组字符之一之前的子字符串 - Negative Lookbehind: Match a substring that's not preceded one of a set of characters 只匹配 @ 而不是 @@ 没有负面的后视 - Match only @ and not @@ without negative lookbehind 如何在没有负面后视的情况下不匹配 RegEx 中的给定前缀? - How to not match given prefix in RegEx without negative lookbehind? 如何在不使用否定回溯的情况下将所有字符串与不以定义字符开头的特定 REGEX 模式匹配 - How to match all strings with a specific REGEX pattern that do not start with a defined character without using a negative lookbehind javascript regexp否定的lookbehind在某些​​情况下不匹配 - javascript regexp negative lookbehind doesn't match in some case 在 Javascript 中向后看:如果之前有多个字符,则不匹配 - Lookbehind in Javascript : don't match if multiple characters before 如何匹配&#39;+ abc&#39;而不是&#39;++ abc&#39;没有lookbehind? - How to match '+abc' but not '++abc' without lookbehind? 正则表达式 - 负面负面观察与br - Regex - negative Negative Lookbehind with br 负正则表达式在Java中落后 - Negative lookbehind regex in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM