...

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



kaisellgren
11-07-2006, 03: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!

kehers
11-08-2006, 08: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]/"

kaisellgren
11-08-2006, 01: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.

kaisellgren
11-14-2006, 10:15 AM
Hmm...

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

kehers
11-14-2006, 10:47 AM
"(\w|[^_])"

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.

kaisellgren
11-14-2006, 07:45 PM
Hi,

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... :(

GJay
11-14-2006, 08:00 PM
the pattern


$pattern='#[a-ä]*#i';

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

kaisellgren
11-15-2006, 08: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...

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


'/\p{L}|\p{Lo}/'

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

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


'/\p{L}|\p{Lo}/'

Hey this worked fine! Thanks! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum