Hello i want to create a regex expression that recognizes a specific pattern (Gendering strings inside article titles). However i am unable to come up with a solution although it should be fairly simple.
I want to recognize one of the letters m w t
followed by the character |
and then again the letters m w t
.
I want the pattern to match an infinite amount of variations between those attributes.
So example patterns that should match are:
m|w
m|w|m
m|w|t|m|m
m|w..................................|t
The patterns that should NOT match for example are:
t
m|
|w|
m|w|
The regex i have come up with so far is ((\|)?(m|w|t){1}[ ]*(\|))+
but of course this wont suit my needs. I'm not sure how to accomplish this.
You can use a repeating group 1+ times
^[mwt](?:\|[mwt])+$
^
Start of string[mwt]
match one of m
w
t
(?:\|[mwt])+
Repeat 1+ times matching |
and again one of m
w
t
$
End of stringWithout the anchors, you can start the match with a word boundary \b
to prevent a partial match, and assert a whitespace boundary to the right at the end of the pattern (?!\S)
If you want to use multiple delimiters, you can use a character class [|\/]
and you only have to escape the forward slash if the pattern delimiters are also /
\b[mwt] *(?:[|\/] *[mwt])+(?!\S)