如何编写regex以从semvar中提取补丁版本

ktca8awb  于 2023-02-17  发布在  其他
关注(0)|答案(1)|浏览(82)

我尝试使用regex从一些semvar中提取v1.2.3的修补程序版本
我有一些正则表达式可以匹配v1.2.部分,但我很难得到另一部分,3(我实际上想要回来)
我使用^v\d+\.\d+\.来选择第一个零件。
我尝试使用负前瞻,然后使用(?!(v\d+\.\d+\.)).*选择它之后的所有内容,但这似乎只是返回v之后的所有内容,而不是group之后的所有内容
任何指示将非常感谢,谢谢!

pbpqsu0x

pbpqsu0x1#

In this special case:
'^(?<=v\d\.\d\.)[[:alnum:]]+'
The regular expression matches as follows:
NodeExplanation
^start of string
(?<=look behind to see if there is:
vv
\ddigits (0-9)
\..
\ddigits (0-9)
\..
)end of look-behind
[[:alnum:]]+any character of: letters and digits (1 or more times (matching the most amount possible))
A more generic solution than works with any length of digits
'^v\d+\.\d+\.\K.[[:alnum:]]+'
The regular expression matches as follows:
NodeExplanation
^start of string
vv
\d+digits (0-9) (1 or more times (matching the most amount possible))
\..
\d+digits (0-9) (1 or more times (matching the most amount possible))
\..
\Kresets the start of the match (what is K ept) as a shorter alternative to using a look-behind assertion: look arounds and Support of K in regex
[[:alnum:]]+any character of: letters and digits (1 or more times (matching the most amount possible))

Check man tr | grep -FA1 '[:' for a POSIX character classes like [[:alnum:]]

相关问题