05. Capture

Groups

Groupings are used when you want to match certain patterns, but only want to extract part of the information.

We can later reference these segments that we captured later on.

To specify a grouping, use parantheses (()).

([cat])\s\1
Matches any string in text where "c c", "a a" or "t t"

Nested groups

You can also nest capture parentheses. The captured groups are in the order in which they are defined.

Referencing

To reference the captured substrings, use a backslash (\).

\0
matches full text
\1
group 1
\2
group 2

Take your Linux skills to the next level!

How Linux Works

Take your Linux skills to the next level! Try Linux & UNIX

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.

$ Check price
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources

Build modern and responsive webpages

JavaScript & JQuery

Build modern and responsive webpages Try Front-end

This book was written for anyone who wants to use JavaScript to make their websites a little more interesting, engaging, interactive, or usable. In particular, it is aimed at people who do not have a degree in computer science (well, not yet anyway).

$ Check price
39.9939.99Amazon 4.5 logo(385+ reviews)

More Front-end resources

Ad