1

I want output "xyz" instead of "www.xyz.com". Mainly I asked this because I wanted to know how to extract something in between the pattern except the pattern itself.

public class Test {
    public static void main(String[] args) {
        Pattern p = Pattern.compile("www[.].+[.]com");
        Matcher m = p.matcher("www.xyz.com");
        if(m.find()){
            System.out.println(m.group());
        }
        in.close();
    }
}
  • Nice question, too. And in case you reach 15 rep and you want to practice upvoting, I am around and glad to help ;-) – GhostCat Jul 10 '17 at 07:24

1 Answers1

0

You ca use \.(.*?)\. which mean any thing between the two dots :

Pattern p = Pattern.compile("www\\.(.*?)\\.com");
Matcher m = p.matcher("I saw a tree. tree was tall. now I will search tree on www.google.com .");
if (m.find()) {
    System.out.println(m.group(1));
}

output

google
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140