java regex extract numbers with boundary and optional space -
i trying extract 12 , 15.
ab cd 12 abc/15 def .*\bab cd\b\s?(\d+)\s?\babc\b[/](\d+)\s?\bdef\b it not working not sure how match exact words. trying match exact words using boundary , seems creating problem.
i tried
.*\\bab cd\\b\\s?(\\d+)\\s?\\babc\\b[/](\\d+)\\s?\\bdef\\b .*\\bab cd\\b\\s*(\\d+)\\s*\\babc\\b[/](\\d+)\\s*\\bdef\\b .*\\bab cd\\b[\\s]?(\\d+)[\\s]?\\babc\\b[/](\\d+)[\\s]?\\bdef\\b .*\\bab cd\\b[\\s]*(\\d+)[\\s]*\\babc\\b[/](\\d+)[\\s]*\\bdef\\b thanks.
besides expression being little redundant, must doing wrong, first expression works:
import java.util.*; import java.util.regex.*; import java.lang.*; class main { public static void main (string[] args) throws java.lang.exception { string currentline = "ab cd 12 abc/15 def"; system.out.println("current line: "+ currentline); pattern p = pattern.compile(".*\\bab cd\\b\\s?(\\d+)\\s?\\babc\\b[/](\\d+)\\s?\\bdef\\b"); matcher m = p.matcher(currentline); while (m.find()) { system.out.println("matched: "+m.group(1)); system.out.println("matched: "+m.group(2)); } } } and demo link prove: http://ideone.com/0txfnu
output:
current line: ab cd 12 abc/15 def matched: 12 matched: 15 so make sure use m.group(number) access each of matched values.
Comments
Post a Comment