2

I need to craft a complex regex expression that will match a varying number of repeating patterns in a string..

An example of the string to match would be as follows

1\(E_123456)\(E_34567)\(E_424324234)\(E_8908908)\(E_23132312)\M_133123 

I have added the parentheses to highlight the repeating patterns I would need to match. These would not be present in the real string.

The order is important as it describes a certain hierarchy. It is this hierarchy I would be looking to extract and construct. The depth of the pattern (or number of repeating patterns) would not be constant at all so it's not a simple case of x number of groups!

ie.. the above has a depth of 5

E_123456, E_34567, E_424324234, E_8908908, E_23132312

while

1\(E_1)\(E_11111)\(E_354534534)\M_133123 

would have a depth of three. (E_1, E_111111, E_354534534)

However I need a regex that will output an ordered list in the order that these values are presented regardless of the depth...

I'm ok with basic regex expressions but they are not my forte so someone's help will be hugely appreciated!

4

2 に答える 2

1

use this regular expression (\\E_\d+)+ extract count by stlit \ or use regex groups

于 2012-07-13T11:41:41.297 に答える
0
var str = @"\E_1\E_11111\E_354534534\M_133123";
var reg = new Regex(@"(\\\w+_\d+)");

foreach (var match in reg.Matches(str))
{
     Console.WriteLine(match);
}
于 2012-07-13T12:25:29.360 に答える