user311509
user311509

Reputation: 2866

Allow Some Special Characters using Regex

The following regex matches any Unicode Letters + Unicode Numbers + Vowel Signs + Dot + Dash + Underscore + Space

/^[\w\pN\pL\pM .-]+$/u

Works successfully.

I want to edit my regex so it accepts the following:

? ! ( ) % @ # , + - : newline

- represents negative sign.

My attempt doesn't work:

/^[\w\pN\pL\pM .-**?!()%@#,+-:\r**]+$/u

Here is my snippet with latest attempt:

if(preg_match('/^[\w\pN\pL\pM .-?!()%@#,+-:\r]+$/u', $_POST['txtarea_msg']))

Any idea?

Upvotes: 1

Views: 813

Answers (3)

Jason McCreary
Jason McCreary

Reputation: 73031

Some of these are regular expression characters, so you need to escape them:

/^[\w\pN\pL\pM .?!()%@#,+\-:\r]+$/u

Also note the difference between a newline (\n) and carriage return (\r).

Upvotes: -1

Korvin Szanto
Korvin Szanto

Reputation: 4511

/^[\w\pN\pL\pM \?!\(\)%@#,\+\-:\n\r]+$/u should do it.

Upvotes: 1

Marc B
Marc B

Reputation: 360872

- is a metacharacter in character classes, so you're saying:

blahblahblah all characters from . to ? blahblahblah all characters from + to : blah blah

It needs to be escaped with a \: blahblah .\-? blahblah +\-: blahblah

Upvotes: 2

Related Questions