0

If i have a line that ends like

version is: 1.10.0.1001

i'm looking for a regex to get the version number but can't figure out how to get the string following "version is:"

1
  • ignore all the inputs upto : and consider the rest. Commented Nov 18, 2011 at 4:24

4 Answers 4

2

Use parantheses to capture values. If you only want the entire version value in one string, this would be enough

if(/version is:(.*)/.test(yourString)) {
    versionNum = RegExp.$1;
}

Here versionNum will store 1.10.0.1001.

But if you wanted the individual numbers between the dots, you would have to go with something like this:

if(/version is:(\d+\.)(\d+\.)(\d+.)(\d+)/.test(yourString)) {
    majorBuild = RegExp.$1;
    minorBuild = RegExp.$2;
    patch = RegExp.$3;
    revision = RegExp.$4;
}

Basically the variables will hold values like this

majorBuild = 1
minorBuild = 10
patch = 0
revision = 1001

Cheers!

Sign up to request clarification or add additional context in comments.

Comments

0

use a capture (parentheses). Try

s/version is:(.*)/\1/g

also, what regex engine are you using? You may need to escape the colon, and determine if magic is on/off

or better yet, just remove version is: and everything before it

s/.*version is://g

1 Comment

what guarantees do you have about your input?
0

Regex using java:

System.out.println("abcd version is:1.23.33.444".replaceAll("^(?:.*:)(.*)$","$1"));
System.out.println("abcd version is:1.23.33.444".replaceAll("^(?:.*:)",""));

Comments

0

Nevermind i figured it out

(?<=version is: ).*

This won't include "version is: " in the match and will take any number of characters after it

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.