1

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.

Vnsax
  • 67
  • 4
  • 1
    Try [`(?[+^%]*(?:\([^)]*\)|{[^}]*}))|(?[\w\s]+)`](http://regexstorm.net/tester?p=%28%3f%3cspecial%3e%5b%2b%5e%25%5d*%28%3f%3a%5c%28%5b%5e%29%5d*%5c%29%7c%7b%5b%5e%7d%5d*%7d%29%29%7c%28%3f%3ctext%3e%5b%5cw%5cs%5d%2b%29&i=Text+1%5e%25%2b%7bTAB%7d%28%7bCMD+1%7d%7bCMD+2%7d%29Text+2.%5e%28abc%29) – Wiktor Stribiżew Sep 10 '18 at 09:46
  • ^(Text 1).*(Text 2).* – Nhan Phan Sep 10 '18 at 09:51

2 Answers2

1

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.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • How would I match +^% without { or ( in the text group in this string? `TEXT 1{SPECIAL 1}TEXT ^+% 2+^%(SPECIAL +^% 2)` – Vnsax Sep 16 '18 at 05:11
  • 1
    @Vnsax It is impossible to match discontinuous texts into one capturing group. You may replace all of those chars with an empty string, e.g. `match.Groups["special"].Value.Replace("[", "").Replace("]", "").Replace("{", "").Replace("}", "")` or `Regex.Replace(match.Groups["special"].Value, @"[][{}]+", "")` – Wiktor Stribiżew Sep 16 '18 at 09:19
0

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();
jdweng
  • 33,250
  • 2
  • 15
  • 20