2016-08-16 67 views
1

我试图与此正则表达式​​使用Java代码来提取软件版本号形式简短的文字如下:正则表达式来提取软件版本

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

但是,我只能不信贝塔提取0.6.00.6.1

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

+0

我能找到最短的版本:'\ d [^ \ S] +' - https:// regex101。com/r/pW8gO5/5 - 编辑:'\ d [^] +'也可以。 – Yaron

回答

1

您可以使用

((?:\d.)+) 

测试字符串

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

匹配更多信息

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

DEMO: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` 

DEMO:https://regex101.com/r/pW8gO5/2


EDIT 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` 

DEMO: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` 

DEMO:https://regex101.com/r/pW8gO5/4

+0

我想提取之前0.6.0.Beta不仅0.6.0 – Sultan

+0

@Sultan请参阅编辑2 –

+0

@Sultan此正则表达式将提取任何版本号前的单词。但是,如果你只想找到'before'之前的单词,那么我们就不得不相应地更新逻辑。 –