I need regular expressions to match the below cases.
I don't think you can 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
).