overlaping比赛.NET正则表达式数 [英] .NET Regex number of overlaping matches
问题描述
我有这样的正则表达式发现任何排列有一个A,一个B和两个C的
(????:(小于A>一种)|(小于B> B)|(小于C> C)) ?{4}(小于 - a取代;?)(小于-B>?)(小于-C&GT){2}
例如对于这个组合,我们有3场比赛(位置1,7,15)
ABCCABCABCABCAABCC
我们需要知道我们有多少overlaping场比赛都有。在当我们发现在第4个位置匹配这种情况下,它开始寻找位置5另一场比赛。
我们需要它来开始寻找在位置2的下一个的比赛,因此比赛将在位置:1,2,3,4,7,10,15
我们不得不7场比赛在这个例子中
1. ABCC
2. BCCA
3. CCAB
4. CABC
7. CABC
10. CABC
15. ABCC
如何使用正则表达式来开始寻找下一个位置的下一场比赛中,而不是完整的secuence之后的下一个位置?
可以在有可能在此先感谢。
您需要使用的 捕获组 内的 < STRONG>前瞻 :
请参阅<一href="http://www.mathworks.com/help/matlab/matlab_prog/lookahead-assertions-in-regular-ex$p$pssions.html"相对=nofollow>这里:
<$p$p><$c$c>(?=(?<value>(?:(?<A>A)|(?<B>B)|(?<C>C)){4}(?<-A>)(?<-B>)(?<-C>){2})) ^ ^向前断言不消耗字符串中的字符。这样一来,你可以用它们来寻找重复的字符序列。
如果你想存储一个超前里面的正则表达式的比赛,你必须把捕捉周围的正则表达式括号超前里面,像这样:(?=(正则表达式))
。
更多细节可以在Rexegg.com找到
请参阅<一href="http://regexstorm.net/tester?p=(%3f%3d(%3f%3cvalue%3e(%3f%3a(%3f%3cA%3eA)%7c(%3f%3cB%3eB)%7c(%3f%3cC%3eC))%7b4%7d(%3f%3c-A%3e)(%3f%3c-B%3e)(%3f%3c-C%3e)%7b2%7d))&i=ABCCABCABCABCAABCC"相对=nofollow>演示
I have this RegEx that finds any permutation with one A, one B and two C's
(?:(?<A>A)|(?<B>B)|(?<C>C)){4}(?<-A>)(?<-B>)(?<-C>){2}
for example for this combination we have 3 matches (positions 1, 7, 15)
ABCCABCABCABCAABCC
We need to know how many overlaping matches we have. In this case when we find a match in the first 4 positions, it starts looking for another match in the position 5.
We need it to start looking for the next match in the position 2, so the matches would be in the positions: 1, 2, 3, 4, 7, 10, 15
We'd have 7 matches in this example
1. ABCC
2. BCCA
3. CCAB
4. CABC
7. CABC
10. CABC
15. ABCC
How can it be possible using RegEx to start looking for the next match in the next position instead in the next position after the complete secuence?
Thanks in advance.
You need to use a capturing group inside the look-ahead:
See here:
Lookahead assertions do not consume any characters in the string. As a result, you can use them to find overlapping character sequences.
(?=(?<value>(?:(?<A>A)|(?<B>B)|(?<C>C)){4}(?<-A>)(?<-B>)(?<-C>){2}))
^ ^
If you want to store the match of the regex inside a lookahead, you have to put capturing parentheses around the regex inside the lookahead, like this:
(?=(regex))
.
More details on overlapping matches using regex can be found at Rexegg.com.
See demo
这篇关于overlaping比赛.NET正则表达式数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!