Ryan Burnham
Ryan Burnham

Reputation: 2669

Regex to get a string following a string

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:"

Upvotes: 0

Views: 59

Answers (4)

Arindam
Arindam

Reputation: 998

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!

Upvotes: 2

Ryan Burnham
Ryan Burnham

Reputation: 2669

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

Upvotes: 0

Prince John Wesley
Prince John Wesley

Reputation: 63688

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("^(?:.*:)",""));

Upvotes: 0

snapfractalpop
snapfractalpop

Reputation: 2134

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

Upvotes: 0

Related Questions