Dear readers in previous blog I have explained about the character classes(basic definition). Now in this tutorial I am going to explain all of you about operations of character classes with Unions, Intersections, Subtractions and how we can use character classes with quantifiers.
Unions of character classes:
Single character class containing 2 or more character classes is called as Union of character classes . For creating a Union, we have to nest one character class inside the other character class.
Eg : [a-f[x-z]]. This union character class matches the letters a,b,c,d,e,f,x,y and z.
Enter the regex: [a-f[x-z]]
Enter input string to search: y
I found the letter "y" starting at index 0 and ending at index 1.
Enter the regex: [a-f[x-z]]
Enter input string to search: k
No match found.
Intersection of character classes:
Single character class containing 2 or more character classes inside one class with the use of && between 2 character classes, hence it is called as intersection of character classes . It will match the characters which are common to all character classes.
Eg: [a-z && [abc]]. This intersection character classes matches the letters only a,b and c.
Enter the regex: [a-z && [abc]]
Enter input string to search: c
I found the letter "c" starting at index 0 and ending at index 1.
Enter the regex: [a-z && [abc]]
Enter input string to search: f
No match found.
Enter the regex: [0-9 && [3-8]]
Enter input string to search: 5
I found the digit "5" starting at index 0 and ending at index 1.
Enter the regex: [0-9 && [3-8]]
Enter input string to search: 2
No match found.
Subtraction of character classes:
Single character class containing 2 or more character classes inside one class with the use of && between 2 character classes and use of ^ in other nested character class, it is called as subtraction of character classes . It will match the characters except that characters which are available in other character class.
Eg: [a-z && [^abc]]. This subtraction character classes matches the letters except abc.
Enter the regex: [a-z && [^abc]]
Enter input string to search: t
I found the letter "t" starting at index 0 and ending at index 1.
Enter the regex: [a-z && [^abc]]
Enter input string to search: b
No match found.
Use of character classes and capturing groups with quantifier:
If regular expression is ‘xyz+’ means it will match x followed by y followed by z one or more times means we can attach a quantifier not only on a single character also attach on a character classes and capturing group such as [xyz]+ , it will match x or y or z one or more times and if regular expression is (xyz)+ , it will match the group (x followed by y followed by z) one or more times.
Eg: Enter the regex: (sri){3}
Enter input string to search: srisrisrisrisrisri
I found the text “srisrisri” starting at index 0 and ending at index 9.
I found the text “srisrisri” starting at index 9 and ending at index 18.
Enter the regex: sri{3}
Enter input string to search: srisrisrisrisrisri
No match found.
In the first example quantifier applies to the entire capturing group sri means s followed by r followed by i 3 times, it will match to the input string, Hence the result will be true.
In the second example remove the parenthesis then regex will s followed by r followed i, here i is 3 times and it will match to the input string, Hence the result will be false.
0 Comment(s)