Reputation: 3582
Can someone please help me to understand how does this program calculate output given below?
import java.util.regex.*;
class Demo{
public static void main(String args[]) {
String name = "abc0x12bxab0X123dpabcq0x3423arcbae0xgfaaagrbcc";
Pattern p = Pattern.compile("[a-c][abc][bca]");
Matcher m = p.matcher(name);
while(m.find()) {
System.out.println(m.start()+"\t"+m.group());
}
}
}
OUTPUT :
0 abc
18 abc
30 cba
38 aaa
43 bcc
Upvotes: 0
Views: 127
Reputation: 9131
Lets look at your sourcecode, because the regexp itself was already well explained in the other answers.
//compiles a regexp pattern, kind of make it useable
Pattern p = Pattern.compile("[a-c][abc][bca]");
//creates a matcher for your regexp pattern. This one is used to find this regexp pattern
//in your actual string name.
Matcher m = p.matcher(name);
//loop while the matcher finds a next substring in name that matches your pattern
while(m.find()) {
//print out the index of the found substring and the substring itself
System.out.println(m.start()+"\t"+m.group());
}
Upvotes: 0
Reputation: 47
It is printing out the place in the string, starting with 0 instead of 1, where the occurrence of each match occurs. That is the first match, "abc" happens in position 0. the second match "abc" happens at string position 18.
essentially it is matching any 3 character string that contains an 'a', 'b', and 'c'.
the pattern could be written as "[a-c]{3}" and you should get the same result.
Upvotes: 0
Reputation: 8386
It simply searches the String
for a match according to the rules specified by "[a-c][abc][bca]"
0 abc --> At position 0, there is [abc].
18 abc --> Exact same thing but at position 18.
30 cba --> At position 30, there is a group of a, b and c (specified by [a-c])
38 aaa --> same as 30
43 bcc --> same as 30
Notice, the counting starts at 0. So the first letter is at position 0, the second ist at position 1 an so on...
For further information about Regex and it's use see: Oracle Tutorial for Regex
Upvotes: 1
Reputation: 32189
This pattern basically matches 3-character words where each letter is either a
,b
, or c
.
It then prints out each matching 3-char sequence along with the index at which it was found.
Hope that helps.
Upvotes: 0
Reputation: 32507
Lets analize:
"[a-c][abc][bca]"
This pattern looks for groups of 3 letters each.
[a-c]
means that first letter has to be between a
and c
so it can be either a
,b
or c
[abc]
means that second letter has to be one of following letters a
,b
or c
co basicly [a-c]
[bca]
meanst that third letter has to be either b
or c
or a
, order rather doesnt matter here.
Everything what you needs to know is in official java regex tutorial http://docs.oracle.com/javase/tutorial/essential/regex/
Upvotes: 1