Reputation: 67
I'm currently using this regex but can figure out how to get the text group to combine its result.
String: Text 1^%+{TAB}({CMD 1}{CMD 2})Text 2.^(abc)
Regex: (?<special>[\^+%]*?[\(][^)]*[\)])|(?<special>[\^+%]*?[\{][^}]*[\}])|(?<text>.)
Result:
text: T
text: e
text: x
text: t
text:
text: 1
special: ^%+{TAB}
special: ({CMD 1}{CMD 2})
text: T
text: e
text: x
text: t
text:
text: 2
special: ^(abc)
Wanted:
text: Text 1
special: ^%+{TAB}
special: ({CMD 1}{CMD 2})
text: Text 2
special: ^(abc)
Ultimately I want "Text 1" and "Text 2" to be two groups in the text group. Can't seem to get the text group not to interfere with the special group when adding .*?(..) for the life of me.
Upvotes: 1
Views: 454
Reputation: 34433
Try following :
string input = "Text 1^%+{TAB}({CMD 1}{CMD 2})Text 2.^(abc)";
string pattern = @"^(?'text1'[^\^]+)(?'special1'[^\(]+)(?'special2'[^\)]+\))(?'text2'[^\^]+)(?'special3'.*)";
Match match = Regex.Match(input, pattern);
Console.WriteLine("Text : '{0}' Special : '{1}' Special : '{2}' Text : '{3}' Special : '{4}'",
match.Groups["text1"].Value,
match.Groups["special1"].Value,
match.Groups["special2"].Value,
match.Groups["text2"].Value,
match.Groups["special3"].Value
);
Console.ReadLine();
Upvotes: 0
Reputation: 627607
You may use
(?<special>[+^%]*(?:\([^)]*\)|{[^}]*}))|(?<text>[\w\s]+)
See the regex demo.
Details
(?<special>[+^%]*(?:\([^)]*\)|{[^}]*}))
- Group "special" capturing:
[+^%]*
- zero or more +
, ^
or %
chars(?:
- a non-capturing group matching either of the two alternatives:
\([^)]*\)
- a (
, then 0+ chars other than )
and then a )
|
- or{[^}]*}
- a {
, then 0+ chars other than }
and then a }
)
- end of the non-capturing group.|
- or(?<text>[\w\s]+)
- Group "text": one or more word or whitespace chars.Upvotes: 1