Computer Science Canada [Regex-tut] Groups Redux: Capturing Groups Intro |
Author: | wtd [ Sat Nov 13, 2004 10:35 pm ] | ||||||||||||
Post subject: | [Regex-tut] Groups Redux: Capturing Groups Intro | ||||||||||||
Recap Earlier I described that you can use sets of parentheses to create groups. For instance:
Matches either:
Or:
But what did it match? So I can say:
But I kinda want to know what I matched. Capturing groups Groups using parentheses are also known as "capturing groups". This is because they serve another purpose. They remember what they matched, or in other words, "capture" it. What they matched gets put into a special set of global variables. In the above case, if the input matches the regular expression, the results of:
Get put into the variable $1. So, to output what we captured:
The numbering scheme continues as you would expect. |