I was wondering if it is possible to simplify the following regular expression
^(?!.*([,\._\- ]).*\1)(?!.*[',\._\- ]{2})(?!.*(['])(?:.*\2){2})[',\._\- \p{L}] $
Constraints
- All of these characters can appear a maximum of one time each:
_-.
- The last character is a white space.
- This character can appear a maximum of two times each:
'
- All special characters here can not appear consecutively.
Details
(?!.*([,\._\- ]).*\1)
- There cannot be 2 or more occurrences of any one character in_-.
(?!.*[',\._\- ]{2})
- No char in the special char group can appear consecutively.(?!.*(['])(?:.*\2){2})
- Single quote special char cannot appear 3 or more times.
CodePudding user response:
You might write the pattern using a single capture group in combination with negated character classes making use of contrast
You don' have to create a capture group (['])
with a backreference \2
to repeat a single quote 3 times, but you can just repeat that char 3 times.
As there do not seem to be '
at the start or at the end, you can use a repeating pattern \p{L} (?:[',._ -]\p{L} )*
to not have consecutive chars that you don't want.
Note that you don't have to escape the '
and _
in the character class, and you can move the -
to the end so that you don't have to escape it.
^(?![^,._ \n-]*([,._ -]).*\1)(?!(?:[^'\n]*'){3})\p{L} (?:[',._ -]\p{L} )*$
Explanation
^
Start of string(?![^,._ \n-]*([,._ -]).*\1)
Assert not 2 of the same chars[,._ -]
(?!(?:[^'\n]*'){3})
Assert not 3 times'
\p{L}
Match 1 times any kind of letter(?:[',._ -]\p{L} )*
Optionally repeat one of[',._ -]
and again 1 times any kind of letter$
End of string