Shorter regex path

This should match only these numbers: 110, 220, 330, 440, 550, 660, 770, 880, 990(not with 000). Is there a shorter way to this regex (pcre)?

/^(1{2}|2{2}|3{2}|4{2}|5{2}|6{2}|7{2}|8{2}|9{2})0$/
+4
source share
2 answers

You can use:

/\b([1-9])\g{1}0\b/

RegEx Demo

Regular Expression Break:

\b      # word boundary
[1-9]   # match digit 1-9 and group them as captured group #1
\g{1}   # back-reference to group #1
0       # match 0
\b      # word boundary
+3
source

The following is a brief regex that should work:

/^([1-9])\1[0]$/

It uses a backlink for the first matching character to match the second. It provides a template 11, 22, 33etc.

Here is an explanation of the characters used in the expression from regex101:

  • ^ Approve position at beginning of line
  • 1st capture group ([1-9])
    • [1-9] matches one character in the range 1 to 9
  • \1 ,
  • [0]
  • $

: https://regex101.com/r/oV6rE7/1

+1

All Articles