gpt4 book ai didi

python - 在python中用正则表达式匹配非常长和复杂的版本号

转载 作者:行者123 更新时间:2023-12-04 01:12:47 28 4
gpt4 key购买 nike

我有很多这样的线。

some text some more text v3.1.0-beta.4 more & more text
some text some v2 build.3 some more text more & more text
some text some v21.1.23456.551436a4 alpha.4 some more text v16.1.2 more & more text
版本号是:
v3.1.0-beta.4
v2 build 3
v21.1.23456.551436a4 alpha.4 and v16.1.2
问题是我的版本号中可以有 beta 或 build 或两者都没有。版本号的可变长度。
这是我现在拥有的,这只能匹配数字。
\d+(?:\.\d+)+
我如何在给定的行中搜索一个或多个版本号的存在,如上面的行所示?

最佳答案

你可以试试这个正则表达式:

\bv[\d+]+[\w.]*(?:[-\s]+(?:alpha|beta|build)[\w.]*)?
RegEx Demo
图案详情:
  • \bv : 匹配 v字边界后
  • [\d+]+ : 匹配 1+ 个数字或点字符
  • [\w.]* : 匹配 0 个或多个单词或点字符
  • (?:[-\s]+(?:alpha|beta|build)[\w.]*)? : 以空格或连字符开头,可选匹配 alpha|beta|build部分后跟 0 个或多个单词或点字符
  • 关于python - 在python中用正则表达式匹配非常长和复杂的版本号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64300951/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com