Aleksandr Makov
Aleksandr Makov

Reputation: 2938

How do I avoid capturing the primary group of a given regex pattern?

I have a regexp pattern:

<^(([a-z]+)\:([0-9]+)\/?.*)$>

How do I avoid capturing the primary group?

<^(?:([a-z]+)\:([0-9]+)\/?.*)$>

The above pattern will still put the whole string 'localhost:8080' into the first (0) group. But I need to get only 2 matched groups, so that first (0) group is populated with 'localhost' and second (1) with '8080'.

Where did I make a mistake?

Upvotes: 0

Views: 92

Answers (5)

Kaii
Kaii

Reputation: 20540

from the docs:

matches

If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

if you don't care about the full match, you can use array_shift() to remove the unwanted element.

array_shift($matches);

Upvotes: 1

earthmeLon
earthmeLon

Reputation: 638

If you are dealing with URLs, you can try using PEAR NetURL, or what might be better for you in this case would be parse-url()

print_r(parse_url($url));

Upvotes: 1

Hersha
Hersha

Reputation: 207

In a regex $0 is always equal to match string and not one of the groupings. Match groups will always start at $1. So look at $1 and $2 instead of $0 and $1.

Upvotes: 1

kitti
kitti

Reputation: 14794

That's just the way the regex functions work. The first group is always the entire match. You can use array_shift to get rid of it.

http://www.php.net/manual/en/function.array-shift.php

Upvotes: 1

Bart Kiers
Bart Kiers

Reputation: 170148

The first group, 0, will always be the entire match.

Upvotes: 3

Related Questions