I am trying to match a version substring with regex in the form of v###.##.### or version #.##.###. The number of version numbers doesn’t matter and there may or may not be a space after the v or version. This is what I was trying so far but it’s not matching in some cases:
\bv\s?[\d.]*\b|\bversion\s?[\d.]*\b
For example, it matches "version 6.2.11" but not c2000_v6.2.11. I’m relatively new to regex and not sure what I’m doing wrong here. I’m pretty sure there’s a better way to do the "or" part as well, so any help would be appreciated thank you!
>Solution :
First, your pattern can be shortened considerably by implementing an optional non-capturing group so that v or version could be matched without the need of an alternation.
Next, the first \b requires a word boundary but the version information starts after _ in the second expected match, and _ is a word char.
You can use
(?<![^\W_])v(?:ersion)?\s?[\d.]*\b
See the regex demo.
Details:
(?<![^\W_])– immediately on the left, there can be no letter or digitv– avchar(?:ersion)?– an optionalersionstring\s?– an optional whitespace[\d.]*– zero or more digits or dots\b– a word boundary.