Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #4035 > unrolled thread

regex help

Started byJerry Stuckle <jstucklex@attglobal.net>
First post2011-12-08 22:51 -0500
Last post2011-12-10 10:01 +0000
Articles 8 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  regex help Jerry Stuckle <jstucklex@attglobal.net> - 2011-12-08 22:51 -0500
    Re: regex help Denis McMahon <denismfmcmahon@gmail.com> - 2011-12-09 11:55 +0000
      Re: regex help Jerry Stuckle <jstucklex@attglobal.net> - 2011-12-09 07:32 -0500
        Re: regex help Jerry Stuckle <jstucklex@attglobal.net> - 2011-12-09 07:51 -0500
    Re: regex help Kim André Akerø <kimandre@nospambetadome.com> - 2011-12-09 14:16 +0100
      Re: regex help Jerry Stuckle <jstucklex@attglobal.net> - 2011-12-09 21:51 -0500
    Re: regex help Curtis Dyer <dyer85@gmail.com> - 2011-12-10 04:25 +0000
      Re: regex help Denis McMahon <denismfmcmahon@gmail.com> - 2011-12-10 10:01 +0000

#4035 — regex help

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-12-08 22:51 -0500
Subjectregex help
Message-ID<jbs0l0$7nj$1@dont-email.me>
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@attglobal.net
==================

[toc] | [next] | [standalone]


#4036

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2011-12-09 11:55 +0000
Message-ID<4ee1f717$0$29383$a8266bb1@newsreader.readnews.com>
In reply to#4035
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

[toc] | [prev] | [next] | [standalone]


#4037

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-12-09 07:32 -0500
Message-ID<jbsv55$h44$1@dont-email.me>
In reply to#4036
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#4038

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-12-09 07:51 -0500
Message-ID<jbt09h$mll$1@dont-email.me>
In reply to#4037
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#4039

FromKim André Akerø <kimandre@nospambetadome.com>
Date2011-12-09 14:16 +0100
Message-ID<op.v574icfkptrp3x@neonk2.intrahouse.local>
In reply to#4035
På Fri, 09 Dec 2011 04:51:56 +0100, skrev Jerry Stuckle  
<jstucklex@attglobal.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@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)

[toc] | [prev] | [next] | [standalone]


#4047

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-12-09 21:51 -0500
Message-ID<jbuhgc$g35$1@dont-email.me>
In reply to#4039
On 12/9/2011 8:16 AM, Kim André Akerø wrote:
> På Fri, 09 Dec 2011 04:51:56 +0100, skrev Jerry Stuckle
> <jstucklex@attglobal.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@attglobal.net
==================

.

[toc] | [prev] | [next] | [standalone]


#4041

FromCurtis Dyer <dyer85@gmail.com>
Date2011-12-10 04:25 +0000
Message-ID<jbumvq$3cr$1@dont-email.me>
In reply to#4035
Jerry Stuckle <jstucklex@attglobal.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);?>

[toc] | [prev] | [next] | [standalone]


#4042

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2011-12-10 10:01 +0000
Message-ID<4ee32dfc$0$29508$a8266bb1@newsreader.readnews.com>
In reply to#4041
On Sat, 10 Dec 2011 04:25:30 +0000, Curtis Dyer wrote:

> Jerry Stuckle <jstucklex@attglobal.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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web