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:
Test
==> Test
Test (1)
==> Test
Test (1) (2)
==> Test (1)
Test (123) (232)
==> Test (123)
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 ?
My guess is that you might likely want to design an expression similar to:
^(.*?)\s*(\(\s*\d+\)\s*)?$
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.
jex.im visualizes regular expressions:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With