We know that we can use group patterns to print the occurence of a particular pattern in a String. We also know that group 0 is the entire String and anyyhing that we put in brackets () becomes group 1 and then if we use another () in our regular expression that becomes group 2 and so on and so forth.
So to print all digits in below String we do the following
Here we did just one group [A-Za-z] rather than two groups as above[A-z][a-z] for any lower or upper case alphabets
Then we use \. for the . and then we put \d+ in paranthesis() .ie we are looking for groups of digits so anything inside () will become group 1
String challenge7 = "abcd.135uvqz.7tzik.999";
Pattern pattern7 = Pattern.compile("[A-Za-z]+\\.(\\d+)");
Matcher matcher7 = pattern7.matcher(challenge7);
//Another way to the above is put abcd. or uvqz. in one group i.e in braces as below so it becomes group 1
//Pattern pattern7 = Pattern.compile("([A-Za-z]+\\.)(\\d+)");
//Then the digits \\d i.e 135 etc will become group 2 and can be accessed as below
// System.out.println("Occurences "+matcher7.group(2));
while(matcher7.find()){
/*Here we are printing group1 for all occurrences of digits. keep in mind group 0 is the entire String and group 1 is the digits*/
System.out.println("Occurences "+matcher7.group(1));
}
Now lets add some tab characters and new line characters to above String
String challenge8 = "abcd.135\tuvqz.7\tzik.999\n";
Then in the below reg expression all we are doing is putting \s in the end to catch all white spaces after the digits and we are putting the digits in a group using (\d+)
Pattern pattern8 = Pattern.compile("[A-Za-z]+\\.(\\d+)\\s");
Matcher matcher8 = pattern8.matcher(challenge8);
while(matcher8.find()){
System.out.println("Occurences "+matcher8.group(1));
}
In below code we are using Pattern to print the indexes for the start and end of group and not the printing the groups itself as above
The end method returns the index of letter after the group ends so in order to print the last digit of group we have to use end(1)-1*/
Pattern pattern9 = Pattern.compile("[A-Za-z]+\\.(\\d+)\\s");
Matcher matcher9 = pattern9.matcher(challenge8);
while(matcher9.find()){
System.out.println("Occurrences start = "+matcher9.start(1)+" end = "+(matcher9.end(1)-1));
}