java - Finding pairs in strings -
java - Finding pairs in strings -
i wondering if can help problem. suppose had string
34342 i find number of pairs in string, two. how go doing that?
edit: ok wanted match occurrences of characters same in string.
you can utilize backreferences find pairs of things appear in row:
(\d+)\1 this match 1 or more digit character followed same sequence again. \1 backreference refers contents of first capturing group.
if want match numbers appear multiple times in string, utilize pattern like
(\d)(?=\d*\1) again we're using backreference, time utilize lookahead well. lookahead zero-width assertion specifies must matched (or not matched, if using negative lookahead) after current position in string, doesn't consume characters or move position regex engine @ in string. in case, assert contents of first capture grouping must found again, though not straight beside first one. specifying \d* within lookahead, considered pair if within same number (so if there's space between numbers, pair won't matched -- if undesired, \d can changed ., match character).
it'll match first 3 , 4 in 34342 , first 1, 2, 3, , 4 in 12332144. note if have odd number of repetitions, match (ie. 1112 match first 2 1s), because lookaheads not consume.
java regex
Comments
Post a Comment