0

First time posting so be gentle. My goal was to match a group of characters (except if numeric) inside the first set of brackets delimited by a comma while ignoring white spaces. The trick was to not match the comma nested in another set of brackets. My solution seems to work so I wanted to share my solution and also get input on any other better solutions.

My example input string is:

test[ [a], b1 ,  1,2 , cc[nn[ 99],oo[ g  , l]],dd].test2[ ee , 22 ]

Desired matches:

1. "[a]"
2. "b1"
3. "cc[nn[ 99],oo[ g  , l]]"
4. "dd"
5. "ee"

Solution:

Regex regexObj = new Regex(
@"(?<=[,\[]\s*)  (?# Match must start with, but not include, opening bracket or comma)
  (?![0-9])      (?# Do not match if followed by a number)
  (?>            (?# Start atomic group, do not backtrack inside the match)
   (?(DEPTH)     (?#  Check IF inside an un-closed bracket group THEN)
    [^\[\]]      (?#   match any char except brackets)
   |             (?#  ELSE closed)
    [^\s,\[\]])  (?#   delimit by comma)
  |              (?# OR)
   (?'DEPTH'\[)  (?#  Match open bracket and increase DEPTH count)
  |              (?# OR)
   (?'-DEPTH'\]) (?#  Match closed bracket and decrease DEPTH count)
  )*             (?# Continue atomic group)
 (?(DEPTH)(?!))  (?# Ensure the match has a balanced set of brackets)
 (?=\s*[,\]])    (?# Match must end with a comma or closing bracket)",
RegexOptions.IgnorePatternWhitespace);

The example I worked from is found here C# Regex - How to remove multiple paired parentheses from string

Community
  • 1
  • 1
AaronV
  • 1
  • 1
  • Hi AaronV, you can create a question and answer it yourself. I suggest to do that way so other users when reading the title can enter to see the solution. Like this, many of them will see zero answers from outside and won't look inside. – Birei Oct 17 '13 at 17:04
  • 1
    @Birei - Will do. Thanks! – AaronV Oct 17 '13 at 18:08

0 Answers0