Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to match 3 or more Consecutive Sequential Characters and Consecutive Identical Characters

I need regular expressions to match the below cases.

  1. 3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc.
  2. 3 or more consecutive identical characters/numbers; e.g. 111, aaa, bbb, 222, etc.
like image 968
sandy0093 Avatar asked Jan 16 '12 12:01

sandy0093


People also ask

What is the regular expression matching one or more specific characters?

The character + in a regular expression means "match the preceding character one or more times". For example A+ matches one or more of character A. The plus character, used in a regular expression, is called a Kleene plus .

What is consecutive identical characters?

Consecutive means one after another. And identical means the exact same. So this means that. There are 3 exact same characters that repeated. “AAA”

What is difference [] and () in regex?

In other words, square brackets match exactly one character. (a-z0-9) will match two characters, the first is one of abcdefghijklmnopqrstuvwxyz , the second is one of 0123456789 , just as if the parenthesis weren't there. The () will allow you to read exactly which characters were matched.

Which are 3 uses of regular expression?

Regular expressions are useful in any scenario that benefits from full or partial pattern matches on strings. These are some common use cases: verify the structure of strings. extract substrings form structured strings.


1 Answers

I don't think you can (easily) use regex for the first case. The second case is easy though:

Pattern pattern = Pattern.compile("([a-z\\d])\\1\\1", Pattern.CASE_INSENSITIVE); 

Since \\1 represents part matched by group 1 this will match any sequence of three identical characters that are either within the range a-z or are digits (\d).

Update

To be clear, you can use regex for the first case. However, the pattern is so laborious and ridiculously convoluted that you are better off not doing it at all. Especially if you wanted to REALLY cover all the alphabet. In that case you should probably generate the pattern programmatically by iterating the char codes of the Unicode charset or something like that and generate groupings for every three consecutive characters. However, you should realize that by having generated such a large decision tree for the pattern matcher, the marching performance is bound to suffer (O(n) where n is the number of groups which is the size of the Unicode charset minus 2).

like image 122
Milad Naseri Avatar answered Oct 14 '22 10:10

Milad Naseri