Java Regex: make it find longest match? -
i having regex (for illustratory purposes only):
(?<number>[0-9]+)|(?<date>[0-9]+\-[0-9]+\-[0-9]+)
applied 2009-11-10
matcher::lookingat
yields 2009
. can pass flag in api or in regular expression tell engine consider longest matching match?
i know in particular case reorder 2 statements, programmatically assembling regular expression several regular expressions , love have order not play matches , not.
i using java 8.
the (general) way re-order groups. in case, know longest match second group needs put first. because regex parsed left right find match.
alternatively, case, can wrap regex boundary matches marking beginning , end of line:
^(([0-9]+)|([0-9]+\-[0-9]+\-[0-9]+))$
this find longest match if input matches whole line.
Comments
Post a Comment