MySQL Regex to match exactly 3 identical characters, but not 4 identical characters in a larger string

I am trying to write a regular expression for mysql in PHP to find (at least one occurrence) exactly 3 of the same characters in a string, but not 4 (or more) of the same.

For example, for "000" I want to find:

  • 0 ////// 0/00 / LS /////// 000
  • 000 //// 0/00 / LS //////////
  • 0 ////// 0/00 / LS //// 000 ///
  • 0 ////// 000 // LS ////// 000 /
  • 0 ////// 000 // LS // 00000000

but not:

  • 0 ////// 000 0 / LS //////////
  • 0 ////// 0 000 / LS //////////
  • 0 ///// 0 000 0 / LS //////////

, , , 3 , , 0, 0 000000

REGEXP '[^0]*[0{3}][^0]*'

.

+4
1

MySQL, . , :

(^|[^0])0{3}([^0]|$)

regex

:

  • (^|[^0]) - , (^) , 0
  • 0{3} - 3
  • ([^0]|$) - , , 0, ($).
+2

All Articles