Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts

    preg_match("/[\w BUT NOT _ \d ]/") how?

    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!
    PHP 5 & MySQL 5 (Y)

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Location
    Nigeria
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    \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]/"

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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.
    PHP 5 & MySQL 5 (Y)

  • #4
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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...
    PHP 5 & MySQL 5 (Y)

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Location
    Nigeria
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    "(\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).
    Code:
    preg_match("/[^\W]+/", $statementMatchingFrom, $Matches);
    Another way im thinking of is negating \d and underscores too
    Code:
    preg_match("/[^(\d|_)]+/", $statementMatchingFrom, $Matches);
    Didnt test them actually but hope at least one help.

  • #6
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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:
    Code:
    "/[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...
    PHP 5 & MySQL 5 (Y)

  • #7
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    the pattern
    PHP Code:
    $pattern='#[a-ä]*#i'
    matches the examples you posted. I'm sure finding out which character comes 'last' shouldn't be too difficult

  • #8
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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...
    PHP 5 & MySQL 5 (Y)

  • #9
    New Coder
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    49
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You could try something like this:

    PHP Code:
    '/\p{L}|\p{Lo}/' 

  • #10
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    if (preg_match('/\w/'$text) && !preg_match('/[_\d]/')){


  • #11
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Nicklas View Post
    You could try something like this:

    PHP Code:
    '/\p{L}|\p{Lo}/' 
    Hey this worked fine! Thanks!
    PHP 5 & MySQL 5 (Y)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •