简体   繁体   English

正则表达式模式从字符串中提取版本号

[英]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." “由于对CVE-2017-7525反序列化漏洞的修复不完整,因此通过2.8.10和2.9.x到2.8.10和2.9.x的FastXML jackson-databind允许未经身份验证的远程代码执行。可通过将恶意制作的JSON输入发送到readValue方法来利用此漏洞。 ObjectMapper,如果存在类路径中有Spring库,则绕过无效的黑名单。”

Note that version number can contain variants like 2.8.x 2.8 2 请注意,版本号可以包含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

I'm not particularly familiar with Scala, so I'm not sure why there are so many quotes around your RegEx. 我对Scala并不是特别熟悉,因此我不确定您的RegEx周围为什么有这么多报价。 I'm going to look past that and try to approach your question using the unescaped RegEx \\d+(\\.\\d+\\.\\d+)+ . 我将超越此范围,尝试使用未转义的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. 也就是说,它将匹配1.2.3以及12.23.34.45 ,但不匹配1.21.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. (?:\\d+|x)是一个非捕获组( ?: :),可以是表示通配符的x或一个或多个数字。

We also use the {0,1} to specify we have either the third group once, or not at all. 我们还使用{0,1}来指定我们只有第三组,或者根本没有。

I hope this helps. 我希望这有帮助。 If you'd like to clarify your requirements, I can modify my answer to fit :) 如果您想澄清您的要求,我可以修改我的答案以适合:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM