[英]Regular expression to parse two numbers from a line
What I want to achieve is actually quite simple : I have lines that look similar to : 我要实现的实际上很简单:我的行看起来类似于:
V123,V20
10,9999
100,V220
etc... 等等...
Basically many pairs of numbers with a leading letter / zero. 基本上,许多数字对都有一个前导字母/零。 I want to be able to parse them using RegEx, what I've found so far in Java is :
我希望能够使用RegEx解析它们,到目前为止,我在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
Any help will be appreciated, Thanks. 任何帮助将不胜感激,谢谢。
public static void main(String[] args) throws IOException {
String[] a = new String[] {"V123,V20", "10,9999", "100,V220"};
for (String s: a) {
Pattern p = Pattern.compile("[\\D0]*(\\d*)[\\D0]*(\\d*)",Pattern.MULTILINE);
Matcher m = p.matcher(s);
System.out.println(m.matches());
System.out.println(m.group(1) + ", " + m.group(2));
}
}
Hope it helps! 希望能帮助到你!
EDIT 编辑
PS: if you have trailing letters, you can add \\\\D in the end: PS:如果有尾随字母,则可以在最后添加\\\\ D:
"[\\D0]*(\\d*)[\\D0]*(\\d*)[\\D]*
I would do it this way 我会这样
Matcher m = Pattern.compile("\\d+").matcher(str);
while(m.find()) {
int i = Integer.parseInt(m.group());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.