Amit Kumar Gupta
Amit Kumar Gupta

Reputation: 7413

Regex - use matching group

I have written a regex

 0[\.\-/]0[\.\-/]0

to compare following patterns;

But it also matches

That I don't want. So is there any way to match the already matched sequence?

 0[\.\-/]0@10

Upvotes: 1

Views: 66

Answers (3)

vks
vks

Reputation: 67988

0([./-])0\\1(0)

You will have to use group and then backreference it using \1.See demo.

https://regex101.com/r/mG8kZ9/3

Upvotes: 1

Claudio
Claudio

Reputation: 69

You may try to use groups like: (0.0.0)|(0/0/0)|(0-0-0)

Upvotes: 0

Amadan
Amadan

Reputation: 198476

Yes, as your title says perfectly: groups. Also called captures. Plain parentheses will capture the submatches; backslash-with-number will refer to the captures in order.

0([\.\-/])0\{1}0

or

0([\.\-/])0\1[0]

(because \10 is something else, so we need to use alternate syntaxes or delimit them properly). \1 (or \{1}) refers to the content between the first open plain parenthesis (i.e. not (?:...), not (?=...), just (...)) and its matching closing one.

Upvotes: 1

Related Questions