Hi friends I have some misgivings with greedy and reluctant
in regex expersions . I ran this piece of code
import java.util.regex.*;
public class Music{
String s = " rap rapture wrap" ;
public static void main(String argv[]){
new Music();
}
Music(){
String regex = "r\\wp?";
Pattern pattern = Pattern.compile(regex);
Matcher matcher =pattern.matcher(s);
while(matcher.find()){
System.out.print(matcher.group());
}
}
}
and the output was rapraprerap with the greedy ? that means 0 o 1 character is reading from
left to right but the book say that greedy read all the string and begin from the end to the
begining.After that I modified the code with the reluctant operator as below
import java.util.regex.*;
public class Music{
String s = " rap rapture wrap" ;
public static void main(String argv[]){
new Music();
}
Music(){
String regex = "r\\wp??";
Pattern pattern = Pattern.compile(regex);
Matcher matcher =pattern.matcher(s);
while(matcher.find()){
System.out.print(matcher.group());
}
}
}
and the output was rararera . I am confused somebody can help me.