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

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -