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

Ace your Technical Interview

Algorithms

Ace your Technical Interview Try Algorithms

This fourth edition of Algorithms is the leading textbook on algorithms today and is widely used in universities worldwide. This book surveys the most important computer algorithms currently in use and provides a full treatment of data structures and algorithms for sorting, searching, graph processing, and string processing.

$ Check price
89.9989.99Amazon 4.5 logo(148+ reviews)

More Algorithms resources

Ad