-2

Presuming I have the following string:

This is a very random string which contains the version number 3.2.1 and I’m looking to extract just the version number. This is a second version number that I don’t want to extract: 5.7.8, and just for kicks, this is a third version number 9.0.1

Would it be possible to extract just the first version number, i.e 3.2.1 using one regex? I’m using JavaScript and although I’ve been able to extract it using two separate regex’s by searching for two strings that surround the version number, then searching in between them for a version number, I haven’t found out how to merge them into one.

Important to note, the string contains lots of version numbers, which come before and after the one I want for extract. However the version number I’m looking to extract always sits between the words number and looking.

Any help is much appreciated.

Thank you

  • It's not clear from your question - what makes the first version number different from the others (other than that it is "the first")? – Sean Bright Oct 07 '19 at 21:38
  • @Unimportant thanks for responding. Sorry for the lack of clarity. The first version number is always in between two specific strings, `number` and `looking` in the example above. The whole string is constant apart from the version number which changes. – ShadowCoder Oct 07 '19 at 21:40

1 Answers1

0

Not adding the /g flag basically just returns the first match! It returns null when there are no matches. You can create non-capturing groups to get only the required value in the match

let string = "This is a very random string which contains the version number 3.2.1 and I’m looking to extract just the version number. This is a second version number that I don’t want to extract: 5.7.8, and just for kicks, this is a third version number 9.0.1";

result = string.match(/number(?:.*)(\d.\d.\d)(?:.*)looking/);

if(result) {
 console.log(result[1]);
}
Dhananjai Pai
  • 5,914
  • 1
  • 10
  • 25