java - Scanner without delimiter -
i able parse strings following: "123456abcd9876az45678". bnf this:
number: ? definition of int ? word: letter { , letter } expression: number { , word , number }
however class java.util.scanner doesn't allow me following:
scanner s = new scanner("-123456abcd9876az45678"); system.out.println(s.nextint()); while (s.hasnext("[a-z]+")) { system.out.println(s.next("[a-z]+")); system.out.println(s.nextint()); }
ideally, should yield:
-123456 abcd 987 az 45678
i hoping java.util.scanner me, looks have create own scanner. there present in java api me?
the question miss information. , therefore answers valid question not problem.
unfortunately cannot use no delimiters scanner class afaik. if wish ignore delimiters, you'd need use methods such findinline()
or findwithinhorizon()
. in case, findwithinhorizion()
appropriate.
scanner s = new scanner("-123456abcd9876az45678"); pattern num = pattern.compile("[+-]?\\d+"); pattern letters = pattern.compile("[a-za-z]+"); system.out.println(s.findwithinhorizon(num, 0)); string str; while ((str = s.findwithinhorizon(letters, 0)) != null) { system.out.println(str); system.out.println(s.findwithinhorizon(num, 0)); }
Comments
Post a Comment