What does $1, $2, etc. mean in Regular Expressions?

When you create a regular expression you have the option of capturing portions of the match and saving them as placeholders. They are numbered starting at $1.

For instance:

/A(\d+)B(\d+)C/

This will capture from A90B3C the values 90 and 3. If you need to group things but don’t want to capture them, use the (?:...) version instead of (...).

The numbers start from left to right in the order the brackets are open. That means:

/A((\d+)B)(\d+)C/

Matching against the same string will capture 90B, 90 and 3.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)