IT. Expert System.

REGEX

Grouping and Capturing


Grouping and Capturing

The grouping constructs delineate the subexpressions of a regular expression and capture the matched substrings of an input string.

These grouping constructs can be used to:

  • Match a subexpression that is repeated in the input string
  • Apply a quantifier to a subexpression
  • Include a subexpression in the string via backreferences
  • Capture and retrieve matched subexpressions

The following grouping construct captures a matched subexpression:

( expression )
Parameter Description
expression Specifies any valid regular expression pattern

Capturing groups are numbered by counting their opening parentheses from left to right, starting from one. In the expression ((A)(B(C))), for example, there are four groups:

  1. (A)(B(C)))
  2. (A)
  3. (B(C))
  4. (C)

Group zero always contains for the entire expression.

Capturing groups can be access in the following ways:

  • By using a backreference within the regular expression
  • By using a named backreference within the regular expression
  • By using the $number sequence

Note: Groups beginning with (?: are non-capturing groups that do not capture text and do not count towards the group total.

Examples

The following example uses grouping, capturing, and a backreference to match duplicate words in a string:

(\w+)\s(\1)

The first subexpression captures multiple word characters, then followed by a whitespace character. Finally, a backreference to the first capturing group to match the same word.

For the text "This is a test test...", this regular expression would produce the following matches:

"This is a test test..."


Content

Android Reference

Java basics

Java Enterprise Edition (EE)

Java Standard Edition (SE)

SQL

HTML

PHP

CSS

Java Script

MYSQL

JQUERY

VBS

REGEX

C

C++

C#

Design patterns

RFC (standard status)

RFC (proposed standard status)

RFC (draft standard status)

RFC (informational status)

RFC (experimental status)

RFC (best current practice status)

RFC (historic status)

RFC (unknown status)

IT dictionary

License.
All information of this service is derived from the free sources and is provided solely in the form of quotations. This service provides information and interfaces solely for the familiarization (not ownership) and under the "as is" condition.
Copyright 2016 © ELTASK.COM. All rights reserved.
Site is optimized for mobile devices.
Downloads: 1455 / 158768827. Delta: 0.00195 с