java - Regular expression to parse two numbers from a line -
what want achieve quite simple : have lines similar :
v123,v20 10,9999 100,v220
etc...
basically many pairs of numbers leading letter / zero. want able parse them using regex, i've found far in 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()); --> 1 returns 0
any appreciated, thanks.
public static void main(string[] args) throws ioexception { string[] = new string[] {"v123,v20", "10,9999", "100,v220"}; (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 helps!
edit
p.s.: if have trailing letters, can add \\d in end:
"[\\d0]*(\\d*)[\\d0]*(\\d*)[\\d]*
Comments
Post a Comment