For example, I am trying to match the following strings
mmmutex
mmuuuTTex
Muuuuuutex
muteeeeeEexxxx
mMutexXxxx
....and many similar strings.
They must have m
, u
, t
, e
and x
in order with one or more characters repeating consecutively 3 and more times.
I found the following solution
^(?i).*([mutex])\\1{2}[mutex]*$
but it also matches
xxxxxx
mmmmmm
ttttt
eeeee
uuuuuuuuu
and many more similar like above which I don't want. See this demo on regex101.com.
CodePudding user response:
Maybe this will do:
^(?i).*?(mmm u t e x |m uuu t e x |m u ttt e x |m u t eee x |m u t e xxx ).*$
What it does is: it ensure that at least one of the letters is repeating 3 or more times.
.*?
ensures that the startingm
characters will enter into the capturing groupmmm u t e x
at least 3m
; orm uuu t e x
at least 3u
; orm u ttt e x
at least 3t
; orm u t eee x
at least 3e
; orm u t e xxx
at least 3x
- everything in a parentheses, from the start
^
till the end$
, but only themutex
variations will be in the captured group\1
.
If your engine does not do case insensitive matching, then you will need this to catch the different cases:
^.*?([Mm][Mm][Mm] [Uu] [Tt] [Ee] [Xx] |[Mm] [Uu][Uu][Uu] [Tt] [Ee] [Xx] |[Mm] [Uu] [Tt][Tt][Tt] [Ee] [Xx] |[Mm] [Uu] [Tt] [Ee][Ee][Ee] [Xx] |[Mm] [Uu] [Tt] [Ee] [Xx][Xx][Xx] ).*$
CodePudding user response:
This seems to work:
(?i)^.*(?=.*(.)\1\1)m u t e x .*$
How this works:
(?i)
: case-insensitive matching^.*
: start of string followed by any char zero or more times(?=
: start of lookahead.*
: any char zero or more times(.)\1\1
: capturing group, capturing any char that is repeated twice)
: end of lookaheadm u t e x
:m
,u
,t
, andx
, each occurring one or more times.*$
: any char zero or more times until end of string
CodePudding user response:
- To achieve consecutive matching characters use:
mutex
- To catch possible repeating character, use
{1,}
which means one or more, or{2,3}
which means between two an three repetitions:
m{1,}u{1,}t{1,}e{1,}x{1,}
Finally:
^(?i)m{1,}u{1,}t{1,}e{1,}x{1,}
CodePudding user response:
Thanks to @bobble bubble The following regex resolved my problem.
^(?i).*(?=\w*?(.)\1\1)\bm u t e x \b.*$
check here https://regex101.com/r/p1Nvde/1