Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to match version substring within overall string

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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 digit
  • v – a v char
  • (?:ersion)? – an optional ersion string
  • \s? – an optional whitespace
  • [\d.]* – zero or more digits or dots
  • \b – a word boundary.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading