regex - Regular Expression for Searching Duplicate Word in java -


i want find repeating word given string. want have regular expression find every occurrence of word. example "i want eat apple. apple fruit".

the regular expression should find out word "apple".

you can use following match duplicate words in line.

(\\b\\w+\\b)(?=.*\\b\\1\\b)        // matches duplicates in single line 

edit: if want match duplicates in multiple lines can use:

(\\b\\w+\\b)(?=[\\s\\s]*\\b\\1\\b)  // or above regex dotall flag 

see demo single line , demo multiple lines


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -