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


Groups > comp.lang.php > #4475

Re: too many matches regex

From Curtis Dyer <dyer85@gmail.com>
Newsgroups comp.lang.php
Subject Re: too many matches regex
Date 2012-01-16 08:30 +0000
Organization A noiseless patient Spider
Message-ID <jf0n6v$mil$1@dont-email.me> (permalink)
References <83c957bb-a286-402c-9466-37a7d20a1dcb@m11g2000yqe.googlegroups.com>

Show all headers | View raw


cate <catebekensail@yahoo.com> wrote:

> $s = '1 2 20 - 31 55 56 77 - 88 97 - 99';
> 
> $patRange = '/((\d+) *- *(\d+))/';

Remove the outermost parentheses. See below.

> $matches = array();
> preg_match_all($patRange, $s, $matches);
> 
> I expected to see an array with 3 matches for each of the there
> number "ranges" found in the
> string $s (20 - 31, 20 and 31)....  I see 4.  Why?  Need some
> insight from a jones.  I don't understand what I'm seeing.
> 
> Thank you.

In the matches returned, you have to account for the overall match 
against the pattern itself. In this case, the outermost subpattern 
is superfluous, since it yields the same result as the overall 
match.

<snip print_r() output of the matches>

-- 
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>

Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

too many matches regex cate <catebekensail@yahoo.com> - 2012-01-15 21:33 -0800
  Re: too many matches regex Michael Fesser <netizen@gmx.de> - 2012-01-16 09:08 +0100
  Re: too many matches regex Curtis Dyer <dyer85@gmail.com> - 2012-01-16 08:30 +0000

csiph-web