java - Print only the first occurrence in a matcher -
i use following code extract age of user in 1 document, age appears several times:
pattern r = pattern.compile("(\\d{2})(?=-year-old)"); matcher matcher = r.matcher("he 55-year-old doctor. xxxxx. 55-year-old man xxxx. when 55-year-old , xxxx"); if(matcher.find()) { system.out.println(matcher.group(0)); }
finally result:
55 55 55
how can print 55
once?
thanks in advance.
you can make non-greedy adding ?
pattern r = pattern.compile("(\\d{2})(?=-year-old)?");
should work, click detail
Comments
Post a Comment