正则表达式,特定seq [英] Regular Expression, specific seq
本文介绍了正则表达式,特定seq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好b $ b
如何匹配以下3组的特定序列。 aB必须在cD和eF之前出现。 cD必须在eF之前。
所有组都可以匹配多次,但是按照指定的顺序。
团体:
aB
cD
eF
匹配:
aBcDeF
aBcDcDeFeF
不配比赛
cDaBeF
我在尝试:
regex =新的正则表达式(@(aB) * \ G(cD)* \(eF)*);
但是它以任何顺序与本集团匹配!
解决方案
当然它以任何顺序匹配组,因为这是你编写正则表达式的方式。这是一个想法:通过在圆括号中按给定顺序包含所有三个组来添加另一个组,并查找该根组的匹配。
- SA
Hi
how do i match a specific sequence of the 3 Groups below. aB must come before cD and eF. cD must come before eF.
All groups can match more than once, but in the specified order.
Groups:
aB
cD
eF
Should Match:
aBcDeF
aBcDcDeFeF
Should not Match
cDaBeF
I was trying:
regex = new Regex(@"(aB)*\G(cD)*\G(eF)*");
but it matches the Group in any order !
解决方案
Of course it matches the group in any order, because this is how you wrote your regular expression. Here is the idea: add another group by enclosing all three groups in the given order in round brackets and look for the match of this root group.
—SA
这篇关于正则表达式,特定seq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文