Regex ignore valid words

I am trying to create a regex expression that starts with a specific word and ignores any other similar words.

For example, if my line begins with the word “dog” and ends with “fish,” how can I ignore any ongoing “dog” words and match only the last?

dog Cat

dog cat-dog <- ignore the first word "dog" and match the second word "dog".

dog dog cat-dog fish <- ignore the first and second words "dog" and match the third word "dog".

+4
source share
3

regex :

(\b\w+\b |\b\w+\b$)(?!\1) m g

: http://regex101.com/r/dW9fP5

:

(\b\w+\b|\b\w+\b$)(?!\1) m g

+3

:

:

(?>(\w+) (?=\1\b))+

test at: regex101, eval.in (if php)

lookahead, , ( ).


, ^ anchor:

^(?>(\w+) (?=\1\b))+

test regex101


EDIT: , , . , 3 :

< > dogdog dogcatcatfish

^(?>(\w{3,})(?=\1))+

test regex101


""

+2

() :

.*(dog)

:

\1 

, . \1

example: http://regex101.com/r/yU6xO8

+1

Source: https://habr.com/ru/post/1536195/


All Articles