Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 !
Posted
Comments
PIEBALDconsult 15-Sep-14 18:36pm    
What about "aBmNcDxYeF" ? If not, then you probably don't want to use \G at least try removing them.

I also notice that although it "matches", it doesn't capture anything, so it's really only matching a empty string.

What about: @"(aB)+(cD)+(eF)+"
Nikolaj jensen 16-Sep-14 5:17am    
hmm, i don't think + will do it, since a group is not required to be present, but can be. + means that it has to be there at least once - right `?

1 solution

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
 
Share this answer
 
Comments
Nikolaj jensen 16-Sep-14 5:23am    
Hi Sergey,

should i add (aBcDeF) ?
How do i look for a "root" group ?
How do i make sure that e.g. subsgroup cd can be present many times ?

like

aBcDcDcDeF

or
aBaBcDcDeFeF
Sergey Alexandrovich Kryukov 16-Sep-14 9:38am    
I don't quite understand. You look for all matches and all groups and consider only the one having all groups inside.
—SA
Nikolaj jensen 16-Sep-14 11:05am    
The important thing for me is to understand how to match a specific order/sequence and not any order of the 3 groups.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900