View Full Version : preg_match("/[\w BUT NOT _ \d ]/") how?

11-07-2006, 04:40 PM
Hi ya,

I need something like this to get working:
preg_match("/[\w_\d]/") how?

The bolded parts should be disincluded, normally \w includes \d and _ but I want to take them off, how do I make it? I know the [^] thing but that's not gonna help...

Thanks for any help!

11-08-2006, 09:35 AM
\w matches for any word character. If you are looking for ways like matching alphabets alone, use somethig like this "/[a-z]/i" . The i flag makes the match case insensitive.
NB - Other thigs can come inside ethe square brackets for example if u want to match numbers as well, it will then go this way "/[a-z0-9]/"

11-08-2006, 02:47 PM
Hold on.

echo preg_match("/\w/","a"); // 1
echo preg_match("/\w/","b"); // 1
echo preg_match("/\w/","ä"); // 1
echo preg_match("/\w/","Ö"); // 1
echo preg_match("/\w/","Å"); // 1
echo preg_match("/\w/","ß"); // 1
echo preg_match("/[a-z]/i","Å"); // 0 This is the reason why im not gonna use [], because I would need to enter MANY characters... the \w has everything that I need luckily, BUT it has too many chars, it has numbers \d and also the underline - I want to take them off...

echo preg_match("/[\w!\d_]/","Å"); // 1

That is incorrect, but I wanted to say you what Im looing for. I wish I could make some exceptions with ! (not) character but that's not doing what I wanted.

11-14-2006, 11:15 AM

I still have no answer for this question. Let me explain this carefully now...

The regular expression should be this in English:
"Match all \w -characters, except the character _"

I have tried "(\w|[^_])" with no luck. Any help?

I know I can just write "/[a-z]/i", but that would NOT include letters such as ä,ö,å,ã, etc...

11-14-2006, 11:47 AM

matches for \w OR a character that is not an underscore. thats why its not working. Av you tried matching characters that are non words? I mean something like this: [^\W] (NB- capital W).

preg_match("/[^\W]+/", $statementMatchingFrom, $Matches);

Another way im thinking of is negating \d and underscores too

preg_match("/[^(\d|_)]+/", $statementMatchingFrom, $Matches);

Didnt test them actually but hope at least one help.

11-14-2006, 08:45 PM

The first one is actually same then \w... and the last one matches all letters plus special charactesr such as "#%/&(+-{} etc...

My current method:

"/[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZðñòóôõöøùúûüýþÿàáâãäåæçèéêëìíîïÐÑÒÓÔÕÖØÙÚÛÜÝÞßÀÁ ÂÃÄÅÆÇÈÉÊËÌÍÎÏ]/"

But as you can see it's very long list... any easier way to do that? the \w would return all those same characters but hte only bad thing is that it includes also numbers and the underline... :(

11-14-2006, 09:00 PM
the pattern


matches the examples you posted. I'm sure finding out which character comes 'last' shouldn't be too difficult

11-15-2006, 09:59 PM
Thank you GJay, but how did you know what is the last character? I wrote a script that echoes all chars from a to 255 with chr and ord...

11-15-2006, 10:46 PM
You could try something like this:


11-16-2006, 01:12 AM
if (preg_match('/\w/', $text) && !preg_match('/[_\d]/')){

11-16-2006, 06:44 AM
You could try something like this:


Hey this worked fine! Thanks! :)