FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » regex help
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: regex help [message #176244 is a reply to message #176243] Sat, 10 December 2011 10:01 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On Sat, 10 Dec 2011 04:25:30 +0000, Curtis Dyer wrote:

> Jerry Stuckle <jstucklex(at)attglobal(dot)net> wrote:
>
>> OK, guys, I'll be the first to admit my regex knowledge is more than
>> limited. And I've never used ereg().
>
> My experience with POSIX regexes is also quite limited, but have you
> tried Google'ing for comparisons between PCRE (PHP's preg_* functions
> are built on top of PCRE) and POSIX regexes?
>
> A quick search turned up: <http://www.regular-
> expressions.info/refflavors.html>, which happens to be a helpful regex
> site I've used in the past.
>
>> So, I need to convert the following ereg() call in some code I'm
>> modifying to a preg() call. Can someone please tell me what the ereg()
>> does and the equivalent preg()?
>>
>> if (ereg("(error</b>:)(.+)(<br)", $buffer, $regs))
>
> With regard to this specific piece of code, I can't see any obvious
> differences in the way this regex pattern behaves in PCRE and POSIX. For
> differences in how each function behaves differently, simply consult the
> PHP documentation (which also contains a section explaining PCRE regex
> syntax).
>
>> TIA.
>
> For a more comprehensive understanding of the two flavors, I would
> settle for nothing less than reading the PCRE manual and the POSIX
> standard regarding its implementation of regexes.

I think the only functional "difference" in the regex itself is that the
PCRE regex requires delimeters, I tend to use "|" when working with html
rather than "/" so that I don't need to escape "/" when it appears as
part of a tag.

ereg doesn't need delimeters inside the regex string, so for example, an
ereg regex of "fred" would be a preg_match regex of "|fred|" or "/
fred/" (or whatever delimeter you wanted to use, preg_match assumes the
first character inside the string is the delimeter to use).

The parenthesised sub-expressions appear to act the same way in both
cases, i.e the part that matches is placed in the array of matches,
which starts with element 0 being the complete matched expression, and
then from left to right, element 1, 2, 3 being the sub-expressions.

My guess is that the original code was extracting the error message
string from formatted html output - in fact it may have been slightly
more economical with ereg to do this:

if (ereg("error</b>:(.+)<br", $buffer, $regs))

$regs[0] will be the complete match
$regs[1] will be the error message string of interest (it was $regs[2]
with the original ereg)
return value will be false (no match) or strlen($regs[0]) (match)

and an equivalent preg_match will (I think, based on my earlier testing)
be:

if (preg_match("|error</b>:(.+)<br|", $buffer, $regs))
return value will be 0 (no match) or 1 (match)

again, $regs[1] is now the error message string of interest, instead of
$regs[2]

Also, ereg is case sensitive, so the i modifier to preg_match isn't
needed.

Rgds

Denis McMahon
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: <beg> ADODB PHP Extension Help </beg>
Next Topic: Edit a record?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 05:43:46 GMT 2024

Total time taken to generate the page: 0.04667 seconds