The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Why doesn't prag_match find "<a"?
Discuss Why doesn't prag_match find "<a"? in the Regex Programming forum on Dev Shed. Why doesn't prag_match find "<a"? Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 30th, 2011, 03:34 PM
|
 |
Contributing User
|
|
|
|
|
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?
|

November 30th, 2011, 04:21 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
If I run that exact code I get the right results...
|

November 30th, 2011, 05:17 PM
|
 |
Contributing User
|
|
|
|
Quote: | 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?
|

November 30th, 2011, 06:26 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Are you saying you've tried the code you posted, character for character, and are getting bad results? What's your exact code?
|

November 30th, 2011, 06:29 PM
|
 |
Contributing User
|
|
|
|
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>)
|

November 30th, 2011, 06:31 PM
|
 |
Contributing User
|
|
|
|
Quote: | 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.
|

November 30th, 2011, 06:54 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
...
You are looking at the HTML source of the page, right?
|

November 30th, 2011, 07:11 PM
|
 |
Contributing User
|
|
|
|
Quote: | Originally Posted by requinix ...
You are looking at the HTML source of the page, right? |
Ahhhh... I am an idiot, lol!!
Thanks requinix
|

December 19th, 2011, 05:27 PM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
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 />";
|

December 20th, 2011, 08:26 AM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
$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.
|

December 20th, 2011, 11:19 PM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
Spot on.
Thanks for pointing that out, I didn't pay enough attention to the details. 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|