Home > Mobile >  PHP Regex for Latin Special Chars but not <>%=
PHP Regex for Latin Special Chars but not <>%=

Time:01-08

I am trying to match latin characters plus things like @ € ; - but not <>%=

I got this pattern working:

^[\p{Latin}€@,.!?\s\/\d\-\ ,.:()?!"„‟'§_]*$ 

But I would like to change it to something like this, to avoid adding all the allowed special chars one by one.

^[\p{Latin}] ?[^#%<>] $

This pattern actually seems th work for strings like

Abc < Cbd (No match)
This is @ works (match)

but as soon as the string starts with a special Char it fails to match

@ work (no match)

It seems to me that the second part of the pattern is allowing anyting but <>%= when the sentence was started with a latin char. I even can write:

a ㄓㄨㄥ ㄨㄣˊ ㄗ dkjfalkfj

and get a match.

It somehow seems logical to me but can't get it and don't know how to get a better way to achive a working pattern.

CodePudding user response:

You can use

^(?:(?![<>%=])[\p{Latin}\s\d\p{P}\p{S}])*$

It matches:

  • ^ - start of a string
  • (?:(?![<>%=])[\p{Latin}\s\d\p{P}\p{S}])* - zero or more occurrences of any char matched with [\p{Latin}\s\d\p{P}\p{S}] character class (Latin letters \p{Latin}, whitespaces \s, digits \d, punctuation proper (\p{P}) and symbols (\p{S})) that is not <, >, % and =
  • $ - end of a string.

See the regex demo.

  •  Tags:  
  • Related