KevinS
KevinS

Reputation: 7875

How do I write a regex for this...?

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.

Upvotes: 2

Views: 51

Answers (3)

Ace Rimmer
Ace Rimmer

Reputation: 141

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 

Upvotes: 0

brads3290
brads3290

Reputation: 2075

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.

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627126

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.

Upvotes: 2

Related Questions