I want to use RegEx in order to remove version numbers from product names. The names can look like this:
SampleProduct 2.0
They can also have parentheses around the version number:
SampleProduct (2.1)
Therefore, I tried to define a group for the version number and an alternative for the version with parentheses:
\s((?<versionNumber>\d+(\.\d+)*)|\(\k<versionNumber>\))\z
But this yields no matches. Since the pattern is quite small I tried accessing the group with an index:
\s((\d+(\.\d+)*)|\(\2\))\z
This didn't work either. By the way, in which order are nested groups indexed? Are they ordered by the occurrence of their open or closing parentheses? Anyways, none of \1
, \2
and \3
yielded a match.
I tried to find out if I misunderstood a feature of Regex. When I experiment with groupings and alternatives seperately, they work as expected. But when I mix these two features, at some point it stops working and I can't spot the problem. Currently I'm successfully using the expression without the grouping to remove the version:
name = name.Substring(0, name.Length - Regex.Match(name, @"\s(\d+(\.\d+)*|\(\d+(\.\d+)*\))\z").Value.Length);
But I would like to aviod the pattern duplication.