I could not seem to figure out the proper regex (java style) to extract just the version part for these string
我似乎无法弄清楚正确的正则表达式(java风格)只提取这些字符串的版本部分
EXPECTATION
spring-aop-4.2.5.RELEASE.jar -> 4.2.5
rumi-1.js -> 1
BouncyCastle-Net-12-1.dll -> 12-1
With the following java regex I keep getting a period at the end of line
使用以下java正则表达式,我会在行尾添加句点
\\b\\d[\\d|\\.|\\-]*\\b
Anyone can suggest a better regex?
任何人都可以建议更好的正则表达式?
FAULTY_RESULT
spring-aop-4.2.5.RELEASE.jar -> 4.2.5.
rumi-1.js -> 1.
BouncyCastle-Net-12-1.dll -> 12-1.
3 个解决方案
#1
2
Digit(s), followed by zero or more lots of a dot/dash and digit(s), not preceded by a word character:
数字,后跟零个或多个点/破折号和数字,前面没有单词字符:
(?<!\w)\d+([.-]\d+)*
In java, it can be done in one line:
在java中,它可以在一行中完成:
String version = packageName.replaceAll(".*?((?<!\\w)\\d+([.-]\\d+)*).*", "$1");
Here, the target term is captured while the regex matches the entire input and the replacement term returns the target (via the captured group).
这里,当正则表达式匹配整个输入并且替换项返回目标(通过捕获的组)时捕获目标项。
#2
0
Probably you can get this expression better, but this is my first attempt. Note. you need to scape this expression.
也许你可以更好地表达这种表达,但这是我的第一次尝试。注意。你需要改变这种表达方式。
(\d+(\.?|-?|\d)+\d|\d)
regards.
问候。
#3
-1
Try something like this:
尝试这样的事情:
\b\d[\d|.|\-]*\d\b
The doubling of the back-slashes goes into the String
constant only:
反斜杠的加倍仅进入String常量:
"\\b\\d[\\d|.|\\-]+*\\d\\b"
However this will not match a single digit version...
但是,这与单个数字版本不匹配......
#1
2
Digit(s), followed by zero or more lots of a dot/dash and digit(s), not preceded by a word character:
数字,后跟零个或多个点/破折号和数字,前面没有单词字符:
(?<!\w)\d+([.-]\d+)*
In java, it can be done in one line:
在java中,它可以在一行中完成:
String version = packageName.replaceAll(".*?((?<!\\w)\\d+([.-]\\d+)*).*", "$1");
Here, the target term is captured while the regex matches the entire input and the replacement term returns the target (via the captured group).
这里,当正则表达式匹配整个输入并且替换项返回目标(通过捕获的组)时捕获目标项。
#2
0
Probably you can get this expression better, but this is my first attempt. Note. you need to scape this expression.
也许你可以更好地表达这种表达,但这是我的第一次尝试。注意。你需要改变这种表达方式。
(\d+(\.?|-?|\d)+\d|\d)
regards.
问候。
#3
-1
Try something like this:
尝试这样的事情:
\b\d[\d|.|\-]*\d\b
The doubling of the back-slashes goes into the String
constant only:
反斜杠的加倍仅进入String常量:
"\\b\\d[\\d|.|\\-]+*\\d\\b"
However this will not match a single digit version...
但是,这与单个数字版本不匹配......