2

Given a string in the format someName_v0001, how do I write a regex that can give me the name (the bit before _v) and version (the bit after _v), where the version suffix is optional.

e.g. for the input

input => (name, version)
abc_v0001 => (abc, 0001)
abc_v10000 => (abc, 10000)
abc_vwx_v0001 => (abc_vwx,1)
abc => (abc, null)

I've tried this...

(.*)_v\(d*) 

... but I don't know how to handle the case of the optional version suffix.

3 Answers 3

2

You can use

^(.*?)(?:_v0*(\d+))?$

See the regex demo.

Details

  • ^ - start of string
  • (.*?) - Capturing group 1: any zero or more chars other than line break chars as few as possible
  • (?:_v0*(\d+))? - an optional sequence of
    • _v - a _v substring
    • 0* - zero or more 0 chars
    • (\d+) - Capturing group 2: any one or more digits
  • $ - end of string.
0
0

To say that a character (or group of characters) is optional, use a ? after it.

For example ABC? would match both “AB” and “ABC”, while AB(CD)? would match both “AB” and “ABCSD”.

I assume you want to make the “_v” part of the version optional as well. In that case, you need to enclose it in a non-capturing group, (?: ), so that you can make it optional using ? without also capturing it.

The correct regex for your scenario is (.*)(?:_v(\d+))?. Capture group 1 will be the name and capture group 2 will be the version, if it exists.

0

You could try:

(.*)_v([\d]*)

The first capture group is name, the second is version.

() => capture result
.* => any character
_v => the string "_v" this should compensate for the special cases
[\d]* => any number of any digits 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.