0

I am trying to come up with a regular expression that will capture a string that contains two instances of "(v#)" (where # can be any number). For example, the string "Test (v6)" would not be captured, but "Test (v6) Word (v2)" would be since it contains two instances of the "(v#)". The furthest I've gotten is:

^.*?(\((v)(\d+)(\))){2}

but that only works if the version numbers ("(v#)") are right next to each other.

UPDATE:

Actually my issue would be solved if I could get the regular expression to make sure that the very end of the string contains (v#). I really want to ignore the middle version number. I know that involves using $ instead of ^ but that's all I have so far.

3 Answers 3

1

If all you really care about is the version at the end, just do this:

\s+\((?<version>v\d+)\)$

And here's an explanation of what's going on.

Any whitespace character 
+ (one or more times)
(
Capture to <version>
  v
  Any digit 
  + (one or more times)
End Capture
)$ (anchor to end of string)
Sign up to request clarification or add additional context in comments.

Comments

0

How about this:

^.*\(\s*v\d+\s*\).*\(\s*v\d+\s*\).*$

Comments

0

Extend your (outer) parentheses to include the .*?, so you have this:

^(.*?\((v)(\d+)(\))){2}

Also, don't know if you are using these capturing groups here (in which case feel free to ignore this) but might be easier to understand what's going one if you got rid of some extraneous parentheses:

^(.*?\(v\d+\)){2}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.