【问题标题】:regex to extract software version正则表达式提取软件版本
【发布时间】:2016-12-22 07:40:46
【问题描述】:

我尝试使用这个正则表达式(?!\\.)(\\d+(\\.\\d+)+)([-.][a-zA-Z]+)?(?![\\d.]) 使用 Java 代码从短文本中提取软件版本号,如下所示:

... Dashbuilder before 0.6.0.Beta allows remote  ... another version 0.6.1 which ...

但是,我只能在没有字母 Beta 的情况下提取 0.6.00.6.1

更新 有时,软件版本带有数字和字符。如何更新我的正则表达式以便能够提取软件版本,例如带字母的数字 (0.6.0.beta) 或仅数字 (0.6.1), 另外,如果我想提取术语before,如果它在使用正则表达式的版本号之前呢?

【问题讨论】:

标签: java regex version


【解决方案1】:

你可以使用

((?:\d.)+)

测试字符串

... Dashbuilder before 0.6.0.Beta allows remote  ...

比赛信息

MATCH 1
1.  [23-29] `0.6.0.`

演示:https://regex101.com/r/pW8gO5/1


编辑

要提取before 0.6.0.Beta,请使用:

(\b\w+\s(?:\d.)+\w+\b)

测试字符串

... Dashbuilder before 0.6.0.Beta allows remote  ...

比赛信息

MATCH 1
1.  [16-33] `before 0.6.0.Beta`

演示:https://regex101.com/r/pW8gO5/2


编辑 2

你可以使用?来匹配可选的before字:

((?:before)?\s(?:\d.)+\w+\b)

测试字符串

... Dashbuilder before 0.6.0.Beta allows remote  ...

... Dashbuilder 0.6.0.Beta allows remote  ...

比赛信息

MATCH 1
1.  [16-33] `before 0.6.0.Beta`
MATCH 2
1.  [69-80] ` 0.6.0.Beta`

演示:https://regex101.com/r/pW8gO5/3


编辑 3

更新以匹配版本。

((?:before)?\s(?:[\d.])+[\w-]+)\b

测试字符串

... Dashbuilder before 0.6.0.Alpha allows remote  ...
... Dashbuilder before 0.6.0.Beta allows remote  ...
... Dashbuilder before 0.6.0.Beta allows remote  ...
... Dashbuilder before 0.6.0 allows remote  ...
... Dashbuilder before 0.6.0.SNAPSHOT allows remote  ...
... Dashbuilder before 0.6.0.RC allows remote  ...
... Dashbuilder before 0.6.0-RELEASE allows remote  ...

比赛信息

MATCH 1
1.  [16-34] `before 0.6.0.Alpha`
MATCH 2
1.  [70-87] `before 0.6.0.Beta`
MATCH 3
1.  [123-140]   `before 0.6.0.Beta`
MATCH 4
1.  [176-188]   `before 0.6.0`
MATCH 5
1.  [224-245]   `before 0.6.0.SNAPSHOT`
MATCH 6
1.  [281-296]   `before 0.6.0.RC`
MATCH 7
1.  [332-352]   `before 0.6.0-RELEASE`

演示:https://regex101.com/r/pW8gO5/4

【讨论】:

  • 我要在 0.6.0.Beta 之前提取,而不仅仅是 0.6.0
  • @Sultan 见编辑 2
  • @Sultan 此正则表达式将提取版本号之前的任何单词。但是,如果您只想找到单词 before,那么我们必须相应地更新逻辑。
  • @Sultan 但是应该有一个标准来区分版本之前的单词和其他单词。你有什么区分标准吗?
  • @Sultan 见 EDIT 2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-16
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多