Home > Back-end >  Regular expression Contains at least 1 special character from the following set, or a non-leading, n
Regular expression Contains at least 1 special character from the following set, or a non-leading, n

Time:10-08

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 ,

Here is the enter image description here enter image description here

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.
  • Related