user1939648
user1939648

Reputation:

Regex non-capturing groups being captured

I have this regex:

"/^(?:[A-Z]+)$/

And this string:

TTT

And the output is:

array (size=1)

0 => string 'TTT' (length=3)

What's wrong? Can anyone point me to the good way doing this?

Upvotes: 0

Views: 75

Answers (1)

Amal Murali
Amal Murali

Reputation: 76646

When you match a string with preg_match(), $matches[0] will contain the entire matched string. The capturing group will be from $matches[1] onwards. In this case, your regex matched the string, so $matches[0] is filled with the whole matched string.

From the vastness of the PHP manual:

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.

Upvotes: 1

Related Questions