Home > Mobile >  How to Get the content inside parenthesis using Regex C#?
How to Get the content inside parenthesis using Regex C#?

Time:03-03

I have the following string and i need to get the content inside the string.

I have tried the following Regex . But it comes with the outer paranthesis.

Input string :

           Test (1001,Sunday)

Regex Pattern

          Regex : (([^,] ),([^,)] )) 
          Output : (1001
       

But I require output like the following

         match[0]=1001 , match[1] = sunday.

I also tried the following patterns but ended up with build warnings.

          (Test \(([^)] )\)) : 

Due to escape sequence i was not able to build it without warnings.

The above Regex works fine for me but i get compilation warnings

CodePudding user response:

If you wanted to do this all with Regex, just match the parenthesis section.

\((\S ?),(\S ?)\)

Then group 1 will be "1001" and group 2 will be "Sunday".

var input = "Test (1001,Sunday)";
var pattern = new Regex(@"\((\S ?),(\S ?)\)");
var match = pattern.Match(input);

Console.WriteLine(match.Groups[1].Value);
Console.WriteLine(match.Groups[2].Value);

In this pattern I'm using \S, which is "any non-whitespace character". You can change this as need be. For example, if you know that the first group is always going to be digits, change to \d.


Another way of doing this that doesn't involve Regex (thanks Trevor for the idea), it to search for the only ( and ) characters, substring, and split by commas.

var input = "Test (1001,Sunday)";
var start = input.IndexOf('(')   1; //  1 to avoid including "("
var end = input.LastIndexOf(')');
var data = input[start..end];
var segments = data.Split(',');

foreach (var segment in segments)
{
    Console.WriteLine(segment);
}

CodePudding user response:

The pattern (([^,] ),([^,)] )) has 3 capture groups, where group 2 for example matches any character except a comma [^,] .

That negated character class will also match the opening (

What you might do is use 2 capture groups instead, exclude the parenthesis as well, and also match the opening and closing parenthesis in the example string.

\(([^,()] ),([^,()] )\)

See a .NET Regex demo and a C# demo.


If there can be more than 2 values between the parenthesis, you can make use of a capture group with the same name, and the Group.Captures Property.

\((?<val>[^,()] )(?:,(?<val>[^,()] ))*\)

The pattern matches:

  • \( Match (
  • (?<val>[^,()] ) Group val, match 1 or more occurrences of any char except , ( )
  • (?: Non capture group to capture as a whole
    • , Match literally
    • (?<val>[^,()] ) Again group val with the same pattern
  • )* Close the non capture group and optionally repeat to also allow a single value
  • \) Match )

See a .NET regex demo and a C# demo.

var pattern = @"\((?<val>[^,()] )(?:,(?<val>[^,()] ))*\)";
var str = "Test (1001,Sunday)";
var strings = Regex.Match(str, pattern).Groups["val"].Captures.Select(c => c.Value);

foreach (String s in strings)
{
    Console.WriteLine(s);
}

Output

1001
Sunday

CodePudding user response:

If your string is always of the pattern

a(b,c)

And you want b and c in different variables it might be simplest to just split on multiple delimiters:

var input = "Test(1001,sunday)";
var bits = input.Split('(',')',',');

Now your "1001" is in bits[1] and "sunday" is in bits[2]


You mentioned that your input is an an array:

var input = new []{
  "Test(1001,sunday)",
  "Test(1002,monday)"
};

So you could:

var r = input.Select(i => {
  var bits = i.Split('(',')',',');
  return new { Num = bits[1], Day = bits[2] };
});

This would produce an enumeration of anonymous types objects where each one has your requested data in its Num/Day properties

foreach(var a in r)
  Console.Write($"Num is {a.Num} and Day is {a.Day}");
  • Related