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

Home » Imported messages » comp.lang.php » too many matches regex
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
too many matches regex [message #176661] Mon, 16 January 2012 05:33 Go to next message
cate is currently offline  cate
Messages: 12
Registered: January 2012
Karma: 0
Junior Member
$s = '1 2 20 - 31 55 56 77 - 88 97 - 99';

$patRange = '/((\d+) *- *(\d+))/';

$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.

Array (PREG_SET_ORDER)
(
[0] => Array
(
[0] => 20 - 31
[1] => 20 - 31
[2] => 20
[3] => 31
)

[1] => Array
(
[0] => 77 - 88
[1] => 77 - 88
[2] => 77
[3] => 88
)

[2] => Array
(
[0] => 97 - 99
[1] => 97 - 99
[2] => 97
[3] => 99
)

)

Array (PREG_PATTERN_ORDER)
(
[0] => Array
(
[0] => 20 - 31
[1] => 77 - 88
[2] => 97 - 99
)

[1] => Array
(
[0] => 20 - 31
[1] => 77 - 88
[2] => 97 - 99
)

[2] => Array
(
[0] => 20
[1] => 77
[2] => 97
)

[3] => Array
(
[0] => 31
[1] => 88
[2] => 99
)

)

Array (null)
(
[0] => Array
(
[0] => 20 - 31
[1] => 77 - 88
[2] => 97 - 99
)

[1] => Array
(
[0] => 20 - 31
[1] => 77 - 88
[2] => 97 - 99
)

[2] => Array
(
[0] => 20
[1] => 77
[2] => 97
)

[3] => Array
(
[0] => 31
[1] => 88
[2] => 99
)

)
Re: too many matches regex [message #176663 is a reply to message #176661] Mon, 16 January 2012 08:08 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(cate)

> $s = '1 2 20 - 31 55 56 77 - 88 97 - 99';
>
> $patRange = '/((\d+) *- *(\d+))/';
>
> $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.

The results of the preg_match_* functions always contain a full pattern
match at the [0] index. The matches from the parentheses then start at
index [1]. Since you've parenthesized the entire pattern, you'll see
that result twice.

Micha

--
http://mfesser.de/blickwinkel
Re: too many matches regex [message #176665 is a reply to message #176661] Mon, 16 January 2012 08:30 Go to previous message
Curtis Dyer is currently offline  Curtis Dyer
Messages: 34
Registered: January 2011
Karma: 0
Member
cate <catebekensail(at)yahoo(dot)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);?>
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: php with openssl in static mode (binaries for windows)
Goto Forum:
  

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

Current Time: Sun Nov 24 21:30:07 GMT 2024

Total time taken to generate the page: 0.02160 seconds