简体   繁体   中英

Regex - use matching group

I have written a regex

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

to compare following patterns;

  • 0.0.0
  • 0-0-0
  • 0/0/0

But it also matches

  • 0.0-0

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

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

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

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

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 (ie not (?:...) , not (?=...) , just (...) ) and its matching closing one.

您可以尝试使用以下组:(0.0.0)|(0/0/0)|(0-0-0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM