tangrs
tangrs

Reputation: 9940

Regex for matching successive strings

What kind of regex should I use if I wanted to match a group of characters but only match if two groups are the same?

For example, the following should match

Hello John, Goodbye John. Hello Amy, Goodbye Amy.

But these should not match

Hello John, Goodbye Amy. Hello Amy, Goodbye John.

Upvotes: 2

Views: 265

Answers (2)

Greg H
Greg H

Reputation: 11

Using backreferences is an easy way to tackle this problem. These are references to previously parenthesized elements in the expression.

For example, the below will only match if the backreference matches "John":

/Hello (John), Goodbye ($1)/

The $1 will ensure the second element matches the first.

More applicable to your problem, the below will allow any word characters (names):

/hello (\w+), goodbye ($1)/gi

Cheers, G

Upvotes: 1

nneonneo
nneonneo

Reputation: 179717

Depending on your regex engine, you can use backreferences in the regex to refer (and match) previously matched items:

Hello (\w+), Goodbye \1.

This produces a match only if the text after "Goodbye" exactly matches the first match (i.e. the word after "Hello").

Example usage (in Python):

>>> re.findall(r'Hello (\w+), Goodbye \1.', 'Hello John, Goodbye John. Hello Amy, Goodbye Amy. ')
['John', 'Amy']
>>> re.findall(r'Hello (\w+), Goodbye \1.', 'Hello John, Goodbye Amy. Hello Amy, Goodbye John. ')
[]

Upvotes: 3

Related Questions