Java 正则表达式来解析一行中的两个数字
我想要实现的其实很简单: 我的线条看起来类似于:Java 正则表达式来解析一行中的两个数字,java,regex,Java,Regex,我想要实现的其实很简单: 我的线条看起来类似于: V123,V20 10,9999 100,V220 等等 基本上是多对带前导字母/零的数字。 我希望能够使用正则表达式解析它们,到目前为止,我在Java中发现的是: Pattern p = Pattern.compile(".*([1-9](?:\\d{0,2})).*",Pattern.MULTILINE); Matcher m = p.matcher("V999,V50"); System.out.println(m.match
V123,V20
10,9999
100,V220
等等
基本上是多对带前导字母/零的数字。
我希望能够使用正则表达式解析它们,到目前为止,我在Java中发现的是:
Pattern p = Pattern.compile(".*([1-9](?:\\d{0,2})).*",Pattern.MULTILINE);
Matcher m = p.matcher("V999,V50");
System.out.println(m.matches()); - return true
System.out.println(m.groupCount()); --> this one returns 0
任何帮助都将不胜感激,
谢谢
希望有帮助
编辑
注意:如果您有尾随字母,可以在末尾添加\\D:
"[\\D0]*(\\d*)[\\D0]*(\\d*)[\\D]*
我会这样做的
Matcher m = Pattern.compile("\\d+").matcher(str);
while(m.find()) {
int i = Integer.parseInt(m.group());
}
(i)拆分和(ii)删除非数字字符不是更简单吗?您想做什么?你所说的只是想“解析它们”。这可能意味着什么。你对上述输入的预期输出是什么?预期输出是两个数字,无论给出的输入(带字母或不带字母)谢谢,这是一个很大的帮助!
Matcher m = Pattern.compile("\\d+").matcher(str);
while(m.find()) {
int i = Integer.parseInt(m.group());
}