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.
The character + in a regular expression means "match the preceding character one or more times".
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.
* - means "0 or more instances of the preceding regex token"
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.
Use backreferences. Eg. in sed:
\([a-zA-Z]\)\1\1
or in PERL regular expressions
([a-zA-Z])\1\1
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With