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();
    }
}
1
  • Nice question, too. And in case you reach 15 rep and you want to practice upvoting, I am around and glad to help ;-) Commented Jul 10, 2017 at 7:24

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

5 Comments

i don't get you @YashBhutoria !
In my case, I have to do this using a paragraph as an input. using this may cause trouble. Suppose this input : "I saw a tree. tree was tall. now I will search tree on www.google.com ." @YCF_L
check my edit @YashBhutoria hope this can help you :)
what does that group one means ?(does this means second match ?) will this work in larger paragraphs ?
@YashBhutoria it mean that groupe between www\\. and \\.com yes i can work in large paragraphe like you already use in your example I saw a tree. tree was tall. now I will search tree on www.google.com .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.