Regexp to match everything "except these"
How to match with regex all special chars except "-" in PHP? - Stack Overflow
Remove All Special Characters Except for a few- RegEx
What regex will match every character except comma ',' or semi-colon ';'? - Stack Overflow
[^-]is not the special character you want[\W]are all special characters as you know[^\w]are all special characters as well - sounds fair?
So therefore [^\w-] is the combination of both: All "special" characters but without -.
\pLmatches any character with the UnicodeLettercharacter property, which is a major general category group; that is, it matches[\p{Ll}\p{Lt}\p{Lu}\p{Lm}\p{Lo}].\pNmatches any character with the UnicodeNumbercharacter property, which is a major general category group; that is, it matches[\p{Nd}\p{Nl}\p{No}].- Note that the Unicode
Alphabeticcharacterproperty also includes certain combining marks such as U+0345 ◌ͅ ᴄᴏᴍʙɪɴɪɴɢ ɢʀᴇᴇᴋ ʏᴘᴏɢᴇɢʀᴀᴍᴍᴇɴɪ. I suggest you that you also include\pM, which matches any character with the UnicodeMarkcharacter property, which is a major general category group; that is, it matches[\p{Mn}\p{Me}\p{Mc}]. - Character U+002D ʜʏᴘʜᴇɴ-ᴍɪɴᴜꜱ is probably the
-you’re referring to. - Note though that Unicode v6.1 has 27 characters with the Unicode
Dashcharacter property, including such common characters as U+2010 ʜʏᴘʜᴇɴ, U+2013 ᴇɴ ᴅᴀꜱʜ, U+2014 ᴇᴍ ᴅᴀꜱʜ, and U+2212 ᴍɪɴᴜꜱ ꜱɪɢɴ. Whether you actually want to include or exclude those, I have no idea.
Given all that, it is not unlikely that you want something like:
[^\pL\pN\pM\x2D\x{2010}-\x{2015}\x{2212}]
[^,;]+
You haven't specified the regex implementation you are using. Most of them have a Split method that takes delimiters and split by them. You might want to use that one with a "normal" (without ^) character class:
[,;]+
Use character classes. A character class beginning with caret will match anything not in the class.
[^,;]
Put _ and . to the negated set of characters ([^...]):
$string = preg_replace('/[^a-zA-Z0-9_.]/', '', $string);
You should not omit $string = .. because preg_replace return replaced string. It does not change the string in place.
You can use some php filter widget like Purifier (to set a whitelist for input)...
But Still, we would like to suggest you to learn regex!
What you really want is this regex for matching:
[^\w\s*]+
Replace it by empty string.
Which means match 1 or more of any character that is:
- Not a word character [AND]
- Not a whitespace [AND]
- Not a literal
*
RegEx Demo
When you define a negative character class, you are really inverting it.
What does that mean ?
A positive character class implicitly OR's it's contents.
When you negate a class, you implicitly AND it's contents.
So, [\w-] means word OR dash,
the inverse, [^\w-] means not word AND not dash.
A negative word for instance, [^\w] would match a dash -.
So, to not match it, you have to add a not dash as well.
A C analogy would be
existing (varA || varB)
inverted (!varA && !varB)
where inverting changes the Boolean of each of the components.
Basically a negative class changes the Boolean of each of its components,
so the implicit OR becomes an implicit AND and the components characters
(or expressions) are negated.
What will really bake your noodle later on is when you see something like
[^\S\r\n]
This translates to NOT-NOT-Whitespace and NOT-cr and NOT-lf
which reduces to matching all whitespace except CR,LF
would you say the below is ok to enter in one special character
Regexp('.*[\¬\!\"\£\$\%\^\&\*\(\)\_\+\`\-\=\{\}\:\@\~\<\>\?\[\]\;\'\#\,\.\/\\\|]'
Yes, your expression will match any string that includes at least one of the specified special characters
hi all,
would you say the below is ok to enter in one special character
Regexp('.*[\¬\!\"\£\$\%\^\&\*\(\)\_\+\`\-\=\{\}\:\@\~\<\>\?\[\]\;\'\#\,\.\/\\\|]'
thanks,
rob