Saturday, 1 November 2014

Naming a capturing group

We can assign a name to capturing group and use it in the Regular expression processing.

Naming Conventions
1. The first character must be a letter.
2. You can use uppercase letters 'A' through 'Z', lowercase letters 'a' through 'z' and digits '0' through '9'.

The newly added RegEx constructs to support the named capturing group are:

(1) (?<NAME>X) to define a named group NAME"
(2) \\k<Name> to backref a named group "NAME"
(3) <$<NAME> to reference to captured group in matcher's replacement str

(4) group(String NAME) to return the captured input subsequence by the given "named group"


import java.util.regex.*;
        
public class CaptureGroupEx {
    public static void main(String args[]){
        String name = "(?<number>\\d+)\\s\\k<number>";
        Matcher m = Pattern.compile(name).matcher("1234 1234");
        
        while (m.find()) {
            System.out.print("I found the text " + m.group());
            System.out.print(" starting at index " + m.start());
            System.out.println(" Ending at index " + m.end());
        }        
    }
}

Output

I found the text 1234 1234 starting at index 0 Ending at index 9


Prevoius                                                 Next                                                 Home

Capturing Groups

Capturing Groups provide a way to specify multiple characters as single unit.

Ex:
(XYZ)
((X)(Y(Z)))

Capturing groups are numbered by counting their opening parentheses from left to right. For Example, in the expression '((Z)(Y(Z)))' there are four capturing groups.

1. ((X)(Y(Z)))
2. (X)
3. (Y(Z))
4. (Z)

Sample Output
Enter Regular Expression
(ab){2}\s(ab)
Enter the string
abab ab
I found the text abab ab starting at index 0 Ending at index 7

Note:
Use this program to run above sample



Prevoius                                                 Next                                                 Home

Matching one of sub expressions

'|'allows you to combine multiple expressions.

Ex:
(fir|la|be)st match strings first, last, best

Run the harness program.

Sample Output
Enter Regular Expression
(fir|la|be)st
Enter the string
I am always last
I found the text last starting at index 12 Ending at index 16

Enter the string
I love best among best
I found the text best starting at index 7 Ending at index 11
I found the text best starting at index 18 Ending at index 22

Enter the string
first come first serve
I found the text first starting at index 0 Ending at index 5
I found the text first starting at index 11 Ending at index 16

Enter the string
first come last serve
I found the text first starting at index 0 Ending at index 5
I found the text last starting at index 11 Ending at index 15

Note:
Use this program to run above sample





Prevoius                                                 Next                                                 Home

Logical operators

Example
Description
XY
X followed by Y
X|Y
Either X or Y
(X)
X, as a capturing group

Note:
Will discuss about the capturing groups in next post.





Prevoius                                                 Next                                                 Home

+ : Match one or more times

Metacharacter '+' match one or more times.

For Example, (ab)+ match ab, abab, ababab etc.,

Enter Regular Expression
(ab)+
Enter the string
ab abab ababab ba
I found the text ab starting at index 0 Ending at index 2
I found the text abab starting at index 3 Ending at index 7
I found the text ababab starting at index 8 Ending at index 14




Prevoius                                                 Next                                                 Home

* : Match zero or more times

Meta character '*' match zero or more times.

For Example, regular expression a*b match b, ab, aab, aaab etc.,


Enter Regular Expression
a*b
Enter the string
b ab aab aab
I found the text b starting at index 0 Ending at index 1
I found the text ab starting at index 2 Ending at index 4
I found the text aab starting at index 5 Ending at index 8
I found the text aab starting at index 9 Ending at index 12



Prevoius                                                 Next                                                 Home

? : once or not at all

you can use metacharacter ? To match a character once or not at all.

For Example 'threat' 'treat' are same except that one has a h and the other doesn't, we can use 'th?reat' to match both. 

Sample Output
Enter Regular Expression
th?reat
Enter the string
treat threat
I found the text treat starting at index 0 Ending at index 5
I found the text threat starting at index 6 Ending at index 12
               
another example, some will type August, some will type Aug. Now we can match both using 'Aug(ust)?'.

Enter Regular Expression
Aug(ust)?
Enter the string
I can type Aug or August my regx match both
I found the text Aug starting at index 11 Ending at index 14
I found the text August starting at index 18 Ending at index 24


Note
Use this program to run above sample



Prevoius                                                 Next                                                 Home