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
Switch to threaded view of this topic Create a new topic Submit Reply
regex help [message #176237] Fri, 09 December 2011 03:51 Go to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
OK, guys, I'll be the first to admit my regex knowledge is more than
limited. And I've never used ereg().

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))

TIA.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: regex help [message #176238 is a reply to message #176237] Fri, 09 December 2011 11:55 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Thu, 08 Dec 2011 22:51:56 -0500, Jerry Stuckle wrote:

> OK, guys, I'll be the first to admit my regex knowledge is more than
> limited. And I've never used ereg().
>
> 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))

Hmmm

By comparing the manual for the two functions:

ereg returns false or 0 if no matches depending on the presence of the
third parameter, or the length of the matched string (which it puts in
$regs[0])

$regs[1], $regs[2] and $regs[3] contain the matched elements ie:

whole match of "/error<\/b>:.+<br/" -> $regs[0]
partial match of "/error<\/b>:/" -> $regs[1]
partial match of "/.+/" -> $regs[2]
partial match of "/<br/" -> $regs[3]
return value is strlen($regs[0])

preg_match returns 0 if no matches, or 1 if a match.

The matched sections for ereg are placed in the $regs array, similar to
$matches array in preg_match

I would suggest:

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

might be equivalent.

Test:

<?php

$strings = array();
$strings[] = "<b>string error</b>: The string was broken<br />";
$strings[] = "<b>string error</b>: The string was broken<br>";
$strings[] = "string error: The string was broken<br />";
$strings[] = "string error: The string was broken<br>";

$regs = array();
$matches = array();

foreach ($strings as $string)
{
echo "test string: '{$string}'\n";
if (ereg("(error</b>:)(.+)(<br)", $string, $regs))
{
echo "ereg result:\n";
print_r($regs);
}
else
{
echo "ereg, no match\n";
}
if (preg_match("|(error</b>:)(.+)(<br)|", $string, $matches))
{
echo "preg_match result:\n";
print_r($matches);
}
else
{
echo "preg_match, no match\n";
}
}

?>

output:

$ php eregvspreg.php
test string: '<b>string error</b>: The string was broken<br />'
ereg result:
Array
(
[0] => error</b>: The string was broken<br
[1] => error</b>:
[2] => The string was broken
[3] => <br
)
preg_match result:
Array
(
[0] => error</b>: The string was broken<br
[1] => error</b>:
[2] => The string was broken
[3] => <br
)
test string: '<b>string error</b>: The string was broken<br>'
ereg result:
Array
(
[0] => error</b>: The string was broken<br
[1] => error</b>:
[2] => The string was broken
[3] => <br
)
preg_match result:
Array
(
[0] => error</b>: The string was broken<br
[1] => error</b>:
[2] => The string was broken
[3] => <br
)
test string: 'string error: The string was broken<br />'
ereg, no match
preg_match, no match
test string: 'string error: The string was broken<br>'
ereg, no match
preg_match, no match
$

I removed the following messages from the output for clarity:

PHP Deprecated: Function ereg() is deprecated in /home/denis/programming/
php/eregvspreg.php on line 15

You may want to add some test cases?

Rgds

Denis McMahon
Re: regex help [message #176239 is a reply to message #176238] Fri, 09 December 2011 12:32 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 12/9/2011 6:55 AM, Denis McMahon wrote:
> On Thu, 08 Dec 2011 22:51:56 -0500, Jerry Stuckle wrote:
>
>> OK, guys, I'll be the first to admit my regex knowledge is more than
>> limited. And I've never used ereg().
>>
>> 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))
>
> Hmmm
>
> By comparing the manual for the two functions:
>
> ereg returns false or 0 if no matches depending on the presence of the
> third parameter, or the length of the matched string (which it puts in
> $regs[0])
>

Hi, Denis,

Unfortunately, it's more than that. preg() uses posix regex syntax,
ereg() uses something else. That's my problem; I don't know the
differences.

If I know what I'm looking for, I can generally fumble around and get a
regex that works (as long as it's not too complicated). But this is in
a generic error handler, and not understanding the ereg() syntax I'm not
exactly positive what it's looking for.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: regex help [message #176240 is a reply to message #176239] Fri, 09 December 2011 12:51 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 12/9/2011 7:32 AM, Jerry Stuckle wrote:
> On 12/9/2011 6:55 AM, Denis McMahon wrote:
>> On Thu, 08 Dec 2011 22:51:56 -0500, Jerry Stuckle wrote:
>>
>>> OK, guys, I'll be the first to admit my regex knowledge is more than
>>> limited. And I've never used ereg().
>>>
>>> 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))
>>
>> Hmmm
>>
>> By comparing the manual for the two functions:
>>
>> ereg returns false or 0 if no matches depending on the presence of the
>> third parameter, or the length of the matched string (which it puts in
>> $regs[0])
>>
>
> Hi, Denis,
>
> Unfortunately, it's more than that. preg() uses posix regex syntax,
> ereg() uses something else. That's my problem; I don't know the
> differences.
>
> If I know what I'm looking for, I can generally fumble around and get a
> regex that works (as long as it's not too complicated). But this is in a
> generic error handler, and not understanding the ereg() syntax I'm not
> exactly positive what it's looking for.
>

Whoops - said that backwards. ereg() uses posix syntax, preg() uses
something else.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: regex help [message #176241 is a reply to message #176237] Fri, 09 December 2011 13:16 Go to previous messageGo to next message
Kim Andr Aker is currently offline  Kim Andr Aker
Messages: 17
Registered: September 2010
Karma: 0
Junior Member
På Fri, 09 Dec 2011 04:51:56 +0100, skrev Jerry Stuckle
<jstucklex(at)attglobal(dot)net>:

> OK, guys, I'll be the first to admit my regex knowledge is more than
> limited. And I've never used ereg().
>
> 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))

ereg() will return the length of the first matched string, if any, or
FALSE if nothing was found. The "if" surrounding it basically is triggered
if the return value is non-false.
On the other hand,preg_match() will return the number of matches (0 or 1,
since it stops after the first match).

Even though they're pretty close, preg_* follows Perl-based regexes, and
as you pointed out earlier, ereg* uses POSIX regexes. Then again, the
regex in your code line above is simple enough to be converted almost
verbatim to preg_* functions, with the addition of a separator and
escaping said separator inside the regex (the forward slash being the most
commonly used):

if (preg_match("/(error<\/b>\:)(.+)(<br)/", $buffer, $regs) == 1)

--
Kim André Akerø
- kimandre(at)NOSPAMbetadome(dot)com
(remove NOSPAM to contact me directly)
Re: regex help [message #176243 is a reply to message #176237] Sat, 10 December 2011 04:25 Go to previous messageGo to next message
Curtis Dyer is currently offline  Curtis Dyer
Messages: 34
Registered: January 2011
Karma: 0
Member
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.

--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
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: 0
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
Re: regex help [message #176249 is a reply to message #176241] Sat, 10 December 2011 02:51 Go to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 12/9/2011 8:16 AM, Kim André Akerø wrote:
> På Fri, 09 Dec 2011 04:51:56 +0100, skrev Jerry Stuckle
> <jstucklex(at)attglobal(dot)net>:
>
>> OK, guys, I'll be the first to admit my regex knowledge is more than
>> limited. And I've never used ereg().
>>
>> 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))
>
> ereg() will return the length of the first matched string, if any, or
> FALSE if nothing was found. The "if" surrounding it basically is
> triggered if the return value is non-false.
> On the other hand,preg_match() will return the number of matches (0 or
> 1, since it stops after the first match).
>
> Even though they're pretty close, preg_* follows Perl-based regexes, and
> as you pointed out earlier, ereg* uses POSIX regexes. Then again, the
> regex in your code line above is simple enough to be converted almost
> verbatim to preg_* functions, with the addition of a separator and
> escaping said separator inside the regex (the forward slash being the
> most commonly used):
>
> if (preg_match("/(error<\/b>\:)(.+)(<br)/", $buffer, $regs) == 1)
>

Thanks, Kim. I do appreciate the help.

It isn't the separators I'm worried about - I've made that mistake
enough times already :). But I have just enough PCRE knowledge to be
dangerous; I can't say that for POSIX regexes.

Thanks again. Looks like it's working just fine, at least for now.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================

..
  Switch to threaded view of this topic Create a new topic Submit Reply
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 07:03:05 GMT 2024

Total time taken to generate the page: 0.01855 seconds