Giorgio
Giorgio

Reputation: 1613

Regular expression - 1st random character equal to the 2nd one.

I'd like to have a regular expression in PHP like "/A.B./i" but the random character following A and the random character following B should be equal.

How can I solve this?

Upvotes: 2

Views: 131

Answers (1)

Milad Naseri
Milad Naseri

Reputation: 4118

This should work:

$regex = '/A(.)B\1/i';

The \1 referencing the captured group (.) is called a back-reference (as @Ilmari Karonen was kind enough to remind).

Upvotes: 6

Related Questions