Requirements: Regular expression Contains at least 1 special character from set, or a non-leading, non-trailing space character.
I am trying to write a regular expression for above requriements, here is what i got so far:
[\^$*."!@#]| (\b\s \b)
The above regular expression could successfully capture the character in the set, but it does not capture the non-trailing and non-leading empty space ,
CodePudding user response:
You can use
[\^$*."!@#]|(?<=\S)\s(?=\S)
Details:
[\^$*."!@#]
- a^
,$
,*
,.
,"
,!
,@
or#
char|
- or(?<=\S)\s(?=\S)
- a whitespace that is both non-leading and non-trailing, since it must be enclosed with non-whitespace chars.