go to previous page   go to home page   go to next page

Does (a+)(a+)\1\1 match the string aaaaa ?


Yes. Group 1 matches the first 'a' and then the second group eats up aaa leaving the end of the string aa for \1\1 to match.

Non-capturing Groups

Regular Expression
Group 0
Group 1 Group 2 Group 3 Group 4

Sometimes you want to use parentheses to group parts of an expression together, but you don't want the group to capture anything from the substring it matches. To do this use (?: and ) to enclose the group.

You might want this in order to make it easier to keep track of backreferences. For example,


matches dollar amounts like $10.43 and USD19.98 and saves the dollar amount in \1 and the cents amount in \2. Now a Java program can refer to the dollar and cents amounts as group 1 and group 2, rather than more obscure numbers. (See chapter 11 for how Java programs can use the values held in backreferences.)

Another benefit of non-capturing groups is that matching is performed faster. Usually the speed up is not noticeable, however.


Will this expression


match the string $44.95 and capture the dollar and cents amount in \1 and \2?