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!

3
  • 1
    The '\' exists on original string? If yes you can just split the string by '\'. str.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); Commented Jul 13, 2012 at 11:40
  • The Regex.Matches method and MatchCollection class are already designed to find multiple matches from a string. Commented Jul 13, 2012 at 11:42
  • @devundef well my lord... sometimes the best solutions are the simplest.. A simple case of over thinking I'm afraid. Would be interesting from an academic viewpoint to see a solution but for expediency; I'll take the pragmatic approach! Thanks Commented Jul 13, 2012 at 11:43

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

If you take off that last '+' sign, you could Regex.Matches it and have an ordered list of each E_ sequence.
@nicholas, it must be chain, simply E_ sequence is not be chain
This does it perfectly. Simple and elegant. Thank you
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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.