Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove optional last parenthesis

Tags:

c#

regex

I'm trying to parse file name and to remove potential number in parenthesis (when having multiple file with same base name), but only the last one

Here are some expected results:

  1. Test ==> Test
  2. Test (1) ==> Test
  3. Test (1) (2) ==> Test (1)
  4. Test (123) (232) ==> Test (123)
  5. Test (1) foo ==> Test (1) foo

I tried to use this regex : (.*)( ?\(\d+\))+, but the test 1 fails.

I also tried : (.*)( ?\(\d+\))? but only the 1st test succeed.

I suspect there's something wrong with quantifiers in the regex, but I didn't find exactly what.

How to fix my regex ?

like image 366
Steve B Avatar asked Dec 17 '22 16:12

Steve B


1 Answers

My guess is that you might likely want to design an expression similar to:

^(.*?)\s*(\(\s*\d+\)\s*)?$

Test

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"^(.*?)\s*(\(\s*\d+\)\s*)?$";
        string input = @"Test
Test (1)
Test (1) (2)
Test (1) (2) (3)
Test (1) (2)    (3) (4) 
";
        RegexOptions options = RegexOptions.Multiline;

        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}

The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

like image 199
Emma Avatar answered Dec 24 '22 00:12

Emma