conikeec
conikeec

Reputation: 209

Regex pattern to extract version numbers from string

I am trying to extract version numbers from a string pattern like this

"FasterXML jackson-databind through 2.8.10 and 2.9.x through 2.9.3 allows unauthenticated remote code execution because of an incomplete fix for the CVE-2017-7525 deserialization flaw. This is exploitable by sending maliciously crafted JSON input to the readValue method of the ObjectMapper, bypassing a blacklist that is ineffective if the Spring libraries are available in the classpath."

Note that version number can contain variants like 2.8.x 2.8 2

and I would want to extract all of them

I need to check this string to verify if my current version matches upto the version specified in the string

val str = "FasterXML jackson-databind through 2.8.10 and 2.9.x through 2.9.3 allows unauthenticated remote code execution because of an incomplete fix for the CVE-2017-7525 deserialization flaw. This is exploitable by sending maliciously crafted JSON input to the readValue method of the ObjectMapper, bypassing a blacklist that is ineffective if the Spring libraries are available in the classpath."
str: String = "FasterXML jackson-databind through 2.8.10 and 2.9.x through 2.9.3 allows unauthenticated remote code execution because of an incomplete fix for the CVE-2017-7525 deserialization flaw. This is exploitable by sending maliciously crafted JSON input to the readValue method of the ObjectMapper, bypassing a blacklist that is ineffective if the Spring libraries are available in the classpath."

val numbers = """"\\d+(\\.\\d+\\.\\d+)+""".r

Upvotes: 0

Views: 1338

Answers (1)

Oliver Dunk
Oliver Dunk

Reputation: 524

I'm not particularly familiar with Scala, so I'm not sure why there are so many quotes around your RegEx. I'm going to look past that and try to approach your question using the unescaped RegEx \d+(\.\d+\.\d+)+.

This will match words that consist of numbers separated by dots, with the restriction that the number of numbers must be odd, and there must be at least three of them.

That is to say, it will match 1.2.3, as well as 12.23.34.45, but not 1.2 or 1.2.3.. The actual match portion will just be the second two digits.

I'm going to guess that you want to match strings that consist of either two or three dot separated numbers, where the second and third can be a wildcard. This should do the trick:

\d+\.(?:\d+|x)(?:\.\d+|x){0,1}

(?:\d+|x) is a non capturing group (?:) that can be either an x representing a wildcard, or one or more digits.

We also use the {0,1} to specify we have either the third group once, or not at all.

I hope this helps. If you'd like to clarify your requirements, I can modify my answer to fit :)

Upvotes: 3

Related Questions