SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: regex stuff

  1. #1
    SitePoint Addict sedna's Avatar
    Join Date
    Jan 2006
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regex stuff

    Hey Guys and Gals,

    I have been trying to learn this regex stuff for ages and just can't seem to get it so i thought maybe practice some stuff, anyway the code shown below dont work and I am not sure why

    PHP Code:
    <?php

    /**
     * @author Cobra Internet
     * @copyright 2008
     */

    $string '1234';

    $match preg_match('/ (\W+) /'$string);

    if(
    $match == true) {
        echo 
    "We Have a Match";
    } else {
        echo 
    "We dont have a mtach";
    }

    ?>
    what I was hoping to do since I was using the \W it should not match a word so my string contains number 1234 so it shoudl echo match found but it dont, any reasson why?

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since \w means it looks for a word in the string. So if you put some words there then it will match. See the difference :
    PHP Code:
    $string '1234';
    $match preg_match('/(\W+)/'$string);
    echo (
    $match == true) ? 'Found' 'Not found'
    and
    PHP Code:
    $string ' world '# or even ' 1234 '
    $match preg_match('/(\W+)/'$string);
    echo (
    $match == true) ? 'Found' 'Not found'
    I am also little learner of this stuff but this is what i have understood.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Addict sedna's Avatar
    Join Date
    Jan 2006
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey rajug thanks for helping me can I ask kindly what does ? : that mean within your echo statement as I am not a pro at php and still learning

  4. #4
    SitePoint Addict mmanders's Avatar
    Join Date
    Jul 2006
    Location
    Edinburgh, Scotland
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's called the ternary operator; many languages have it...

    <boolean_expression> ? <if_true> : <if_false>

    is equivalent to:

    if (<boolean_expression)
    {
    <if_true>;
    }
    else
    {
    <if_false>;
    }

  5. #5
    SitePoint Addict sedna's Avatar
    Join Date
    Jan 2006
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Max

  6. #6
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mmanders View Post
    It's called the ternary operator; many languages have it...

    <boolean_expression> ? <if_true> : <if_false>

    is equivalent to:

    if (<boolean_expression)
    {
    <if_true>;
    }
    else
    {
    <if_false>;
    }
    Not quite.

    You cannot for example do:

    (x == 3) ? y = 5 : y = 4;

    It would be:

    y = (x == 3) ? 5 : 4;

    The if_true and if_false are values that are returned, not expressions that are evaluated like in the if/else block.

    As for the regex:

    rajug: \w does mean word, but \W is the opposite - any non word character. So as long as numbers don't count as a word character (which I'm not sure on), this is right.

    I suspect the reason it isn't working is because you have spaces in the regex, which aren't in the string you are trying to match. Try matching ' 1234 ', or remove the spaces from the regex. There is a modifier (I forget which) which makes the regex engine ignore any whitespace in the expression if that is any help as well.

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    rajug: \w does mean word, but \W is the opposite - any non word character. So as long as numbers don't count as a word character (which I'm not sure on), this is right.
    Ummm yes agreed on that. Sorry i did not notice that \W is opposite of \w. As i said before in the post that i am also a learner of this regex stuff so couldnt go in that depth.

    BTW, can anyone suggest me the complete regex tutorials in the net? You Stormrider??
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  8. #8
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    http://www.regular-expressions.info is pretty good, it's what I used. I'm by no means an expert though, I can't handle lookaheads and that kind of stuff without looking it up!

  9. #9
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,933
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sedna View Post
    PHP Code:
    if($match == true
    Why not just:
    PHP Code:
    if($match
    $match will never equal true because the return values are 0 and 1.


Bookmarks

Posting Permissions

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