Reputation: 5240
I'm trying to extract website address from a url using following code
public String getWebSiteAddress(String text)
{
Pattern p = Pattern.compile("\\b([a-z0-9]+(-[a-z0-9]+)*\\.)+[a-z]{2,3}\\b");
//Pattern p = Pattern.compile("^http\\:/[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(/\\S*)?$");
System.out.println("\\b([a-z0-9]+(-[a-z0-9]+)*\\.)+[a-z]{2,3}\\b");
Matcher m = p.matcher(text);
if(m.matches())
{
System.out.println("got it");
return m.group(0) ;
}
else
{
System.out.println("ddnt get");
return "";
}
}
turns out this code works flawlessly inside regexBuddy but in JAVA can anyone help me what is the problem with my regex or should I change something in my regex according to java?
the website I want to extract is something like :
Upvotes: 0
Views: 90
Reputation: 4472
You may need to use the find() method of matcher.
public String getWebSiteAddress(String text)
{
Pattern p = Pattern.compile("\\b([a-z0-9]+(-[a-z0-9]+)*\\.)+[a-z]{2,3}\\b");
Matcher m = p.matcher(text);
if (m.find()) {
System.out.println("got it");
return m.group(1) ;
}
else
{
System.out.println("didnt get");
return "";
}
}
Matches apparently only works if the entire string matches the regex.
Upvotes: 1