Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match character repeated three times

Tags:

regex

I need to simplify the following regular expression to include all the letters of the alphabet:

(a{3})|(b{3})|(c{3})|(z{3})|(A{3})|(B{3})|(C{3})|(Z{3})

In practice I want to find all the sequences of same three chars, for example:

aaa
bbb
nnn
VVV
JJJ

and so on.

like image 827
user990366 Avatar asked Oct 11 '11 21:10

user990366


People also ask

Which symbol is used in regular expressions which will repeat the previous character one or more number of times?

The character + in a regular expression means "match the preceding character one or more times".

What does the plus character [+] do in regex?

A regular expression followed by an asterisk ( * ) matches zero or more occurrences of the regular expression. If there is any choice, the first matching string in a line is used. A regular expression followed by a plus sign ( + ) matches one or more occurrences of the one-character regular expression.

Why * is used in regex?

* - means "0 or more instances of the preceding regex token"

Which regex matches one or more digits?

Occurrence Indicators (or Repetition Operators): +: one or more ( 1+ ), e.g., [0-9]+ matches one or more digits such as '123' , '000' . *: zero or more ( 0+ ), e.g., [0-9]* matches zero or more digits.


2 Answers

Use backreferences. Eg. in sed:

\([a-zA-Z]\)\1\1

or in PERL regular expressions

([a-zA-Z])\1\1
like image 141
Tomas Avatar answered Nov 15 '22 07:11

Tomas


A regular expression using backreferences would be suitable.

([a-z])\1{2}

So something along the lines of preg_match('/([a-z])\1{2}/i', $string); would suffice.

like image 29
Ben Swinburne Avatar answered Nov 15 '22 06:11

Ben Swinburne