Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx to find two or more consecutive chars

Tags:

regex

People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

What is multiline regex?

Multiline option, or the m inline option, enables the regular expression engine to handle an input string that consists of multiple lines. It changes the interpretation of the ^ and $ language elements so that they match the beginning and end of a line, instead of the beginning and end of the input string.

What is Alnum in regex?

The Alphanumericals are a combination of alphabetical [a-zA-Z] and numerical [0-9] characters, 62 characters.

What does ++ mean in regex?

++ From What is double plus in regular expressions? That's a Possessive Quantifier. It basically means that if the regex engine fails matching later, it will not go back and try to undo the matches it made here.


This should do the trick:

[a-zA-Z]{2,}

[a-zA-Z]{2,} does not work for two or more identical consecutive characters. To do that, you should capture any character and then repeat the capture like this:

(.)\1

The parenthesis captures the . which represents any character and \1 is the result of the capture - basically looking for a consecutive repeat of that character. If you wish to be specific on what characters you wish to find are identical consecutive, just replace the "any character" with a character class...

([a-zA-Z])\1

Finds a consecutive repeating lower or upper case letter. Matches on "abbc123" and not "abc1223". To allow for a space between them (i.e. a ab), then include an optional space in the regex between the captured character and the repeat...

([a-z]A-Z])\s?\1


Personnaly (as a nooby) I've used:

[0-9][0-9]+.

But the one from Simon, is way better ! =D