本文介绍了捕获,如果在子串“"之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(?!([^"]*"[^"]*")*[^"]*$),

这捕获,如果它在"之间.假设测试字符串是

This captures , if it is between "".Lets say the test string is

 1 2 3 4 , 5 6 7, 8 9 "10 11 12 , 13 14 15," 16,17,"18,19,"20 21,22

捕获将在 12 到 13 之间,其余时间为

captured will be between 12 and 13 and rest like

  http://regex101.com/r/rX0dM7/1

现在,如果我将相同的更改为

Now if i change the same the to

(?!(.*?".*?")*[^"]*?$),

这仅捕获结尾,介于 18 和 19 之间.类似于

This captures only the end ,'s between 18 and 19.Something like

  http://regex101.com/r/hL7uS1/1

现在的问题是为什么 [^"] 与 .* 如此不同?".

Now the question is why is [^"] so different from .*?" .

其次,[^"]*$ 的意义是什么,就好像我删除它什么都没有被捕获.

Secondly what is the significance of [^"]*$ as if i remove it nothing gets captured.

推荐答案

从概念上讲,我认为您没有正确考虑这一点.

Conceptually, I think you are not thinking this correctly.

Lookarounds 以当前搜索的位置为中心Position.

Lookarounds are centric about where the current search Position is.

在您的前瞻中,您在表达式中甚至在找到逗号之前就否定匹配该逗号.

In your lookahead, you are negatively matching the comma in an expression before it even finds the comma.

这称为重叠.

Lookahead 通常在匹配的子表达式被消耗后插入,位置
增加然后断言被检查.

Lookahead's usually are inserted after a matched subexpession has been consumed, the position
increases then the assertion is checked.

同样,Lookbehinds 通常位于对象子表达式之前.

Likewise, Lookbehinds typically go before the object subexpression.

所以,你的正则表达式实际上是这样的

So, your regex is actually this

 ,
 (?!
      ( [^"]* " [^"]* " )*
      [^"]* $
 )

当你这样做的时候,你可以很容易地看到去掉[^"]*$
this ( [^"]* " [^"]* " )* 匹配字符串中的每个点.
因为它是可选的.

When you do this, you can easily see that after removing [^"]*$
this ( [^"]* " [^"]* " )* matches at every point in the string.
Because it is optional.

如果你把它改成( [^"]* " [^"]* " )+ 那么它会找到
一些具体的负面匹配.
$ 以前就是为了这个目的.

If you were to change it to ( [^"]* " [^"]* " )+ then it would find
something concrete to negatively match against.
The $ was serving that purpose before.

希望你现在有更好的理解.

Hope you have a better understanding now.

这篇关于捕获,如果在子串“"之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 06:00