#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2008
    Posts
    577
    Rep Power
    181

    Why doesn't prag_match find "<a"?


    Alright, I haven't done much regex in my work which hasn't really been needed in my projects and when it has I just copy and paste regex for what is needed. But I ran in to a simple problem when testing some regex and I'm wondering what is causing it. In php if I do this

    PHP Code:
    $word "<a";
    $pattern '/<a/';

    preg_match($pattern,$word,$matches);

    print_r($matches); 
    This shows up as blank. If I change $word and $pattern to "<>" or "<" or "a" it will find a match.

    Is there something special about the "<" symbol in regex?
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,113
    Rep Power
    9398
    If I run that exact code I get the right results...
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2008
    Posts
    577
    Rep Power
    181
    Originally Posted by requinix
    If I run that exact code I get the right results...
    That's what I don't understand. Can you think of anything that can be causing this in my configuration? I've tried it 50 times and $matches shows up empty. If I try any of those other combinations I get the right result.

    It only happens if I have a "<" that starts off the prag_match. If I do "<a>" it shows up blank also. But if I do "<>" it will find a match. Any thoughts?
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,113
    Rep Power
    9398
    Are you saying you've tried the code you posted, character for character, and are getting bad results? What's your exact code?
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2008
    Posts
    577
    Rep Power
    181
    Yea, this is definitely weird. It has something to do with the "<". I also tried the following and one matches the whole string and the other matches just the first letter.

    PHP Code:

    $word 
    "a<b>";
    $pattern "/a<b>/";

    preg_match($pattern,$word,$matches);

    print_r($matches);
    //Array shows up Array([0]=> a) 
    So that just reads the letter a

    If I do this, it shows up with a correct match

    PHP Code:

    $word 
    "ab>";
    $pattern "/ab>/";

    preg_match($pattern,$word,$matches);

    print_r($matches);
    //Array shows up Array([0]=> ab>) 
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2008
    Posts
    577
    Rep Power
    181
    Originally Posted by requinix
    Are you saying you've tried the code you posted, character for character, and are getting bad results? What's your exact code?
    That is my exact code. I have tried it every way possible, and it shows up blank every time.
  12. #7
  13. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,113
    Rep Power
    9398
    ...

    You are looking at the HTML source of the page, right?
  14. #8
  15. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2008
    Posts
    577
    Rep Power
    181
    Originally Posted by requinix
    ...

    You are looking at the HTML source of the page, right?
    Ahhhh... I am an idiot, lol!!

    Thanks requinix
  16. #9
  17. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    To complete the answer in case someone else is on the same track. This is a common phenomenon when pregmatching html and outputting it to the page to test the expression.

    The easy fix is to use htmlentities.
    So here, something like:
    PHP Code:
    echo htmlentities($matches)."<br />"
  18. #10
  19. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    $matches is an array in this case, so you'll have to use:
    PHP Code:
    echo htmlentities(print_r($matches,1))."<br />\n"
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  20. #11
  21. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    Spot on.
    Thanks for pointing that out, I didn't pay enough attention to the details.

IMN logo majestic logo threadwatch logo seochat tools logo