站内搜索: 请输入搜索关键词
当前页面: 图书首页 > Java Regular Expressions: Taming the java.util.regex Engine

FAQs - Java Regular Expressions: Taming the java.util.regex Engine

Previous Section Next Section

FAQs

Q:?/b>

How do I make a group noncapturing?

To make a group noncapturing, insert ?: inside the opening parenthesis of the group. For example, change (\w) to (?:\w) .

Q:?/b>

Given the expression \w(\d(\w)), what's the capturing index of the rightmost subgroup, (\w)?

Groups are counted from left to right, starting with the opening parenthesis, and group(0) always refers to the whole regex pattern. Thus, the capturing index of subgroup (\w) is 2. Accordingly, the capturing index of (\d(\w)) is group(2) .

Q:?/b>

How is group indexing affected when one of the groups is a noncapturing group?

The noncapturing group isn't counted in any way when group indexes are calculated.

Answers

A:?/p>

To make a group noncapturing, insert ?: inside the opening parenthesis of the group. For example, change (\w) to (?:\w).

A:?/p>

Groups are counted from left to right, starting with the opening parenthesis, and group(0) always refers to the whole regex pattern. Thus, the capturing index of subgroup (\w) is 2. Accordingly, the capturing index of (\d(\w)) is group(2).

A:?/p>

The noncapturing group isn't counted in any way when group indexes are calculated.


Previous Section Next Section