Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

8. Matching with Regular Expressions > The Match Variables

The Match Variables

Parentheses normally trigger the regular expression engine’s capturing features. The capture groups hold the part of the string matched by the part of the pattern inside parentheses. If there is more than one pair of parentheses, there will be more than one capture group. Each regular expression capture holds part of the original string, not part of the pattern. You could refer to these groups in your pattern using back references, but these groups also stick around after the match as the capture variables.

Since these variables hold strings, they are scalar variables; in Perl, they have names like $1 and $2. There are as many of these variables as there are pairs of capturing parentheses in the pattern. As you’d expect, $4 means the string matched by the fourth set of parentheses. This is the same string that the back reference \4 would refer to during the pattern match. But these aren’t two different names for the same thing; \4 refers back to the capture during the pattern while it is trying to match, while $4 refers to the capture of an already completed pattern match. For more information on back references, see the perlre documentation.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial