tags:

views:

39

answers:

2

If I have a string such as "abcdef{123}ghi{456}kl", I want to create a regex that will give me all the parts separated as follows:

abcdef
{123}
ghi
{456}
kl

I am using this code, but can't figure out what the expression should be:

System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex("expression");
foreach (System.Text.RegularExpressions.Match match in rex.Matches(sText).OfType<System.Text.RegularExpressions.Match>())
{
     ...  
}
+3  A: 

You should probably use using statements instead of writing out the namespace in full every time. At first glance your code looks quite complex but when you remove all the namespaces it turns out to be very simple. Also the OfType is not needed.

The regular expression should match as much as possible which isn't an open brace [^{]* or else an open brace, some text and then a closing brace {[^}]*}. The regular expression for this is:

{[^}]*}|[^{]*

Try this code:

string text = "abcdef{123}ghi{456}kl";
Regex regex = new Regex("{[^}]*}|[^{]*");
foreach (Match match in regex.Matches(text))
{
    Console.WriteLine(match.Value);
}

Output:

abcdef
{123}
ghi
{456}
kl

Note: this regular expression does not validate that the string is in the correct format, it assumes that it is well-formed.

A slightly simpler method is to use Split instead of Matches and include a capturing group in the regular expression so that the separator is also included in the output:

string text = "abcdef{123}ghi{456}kl";
Regex regex = new Regex("({[^}]*})");
foreach (string part in regex.Split(text))
{
    Console.WriteLine(part);
}

The output for this is the same as above.

Mark Byers
This is a good start. I've discovered some edge cases I need to account for. For example in "a{1}b{2}c{d{3}d" should return a,{1},b{2},c{d,{3},d but it returns a,{1},b,{2},c,{d{3},d
Jeremy
In that case, go with the Split solution but change the regex to `({[^{}]*})`.
Alan Moore
A: 
([a-z]+)({\d+})([a-z]+)({\d+})([a-z]+)

will work, but only if there are always five parts in the string. Can there be fewer/more than five?

Richard Fearn
There could be any number
Jeremy