Solved

How to use regex to get things out of hostname

Posted on 2011-09-20
10
287 Views
Last Modified: 2012-05-12
Hi,

I would like to use ONE regular expression to get abc.com or abc.com.cn out of all the following host names:

1) www.abc.com
2) www.abc.com.cn
3) www.xyz.abc.com
4) www.xyz.abc.com.cn
5) xyz.abc.com
6) xyz.abc.com.cn

Thanks!
0
Comment
Question by:wsyy
10 Comments
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
http://www.exampledepot.com/egs/java.lang/HasSubstr.html

just check

if (string.indexOf("abc.com") != -1 }|| string.indexOf("abc.com") != -1 )
{
   //string is containing required substrings
}
0
 
LVL 4

Expert Comment

by:stachenov
Comment Utility
Something like this works:
 
String[] t = {"www.abc.com", 
            "www.abc.com.cn", 
            "www.xyz.abc.com", 
            "www.xyz.abc.com.cn",
            "xyz.abc.com", 
            "xyz.abc.com.cn",
        };
        Pattern p = Pattern.compile("((?:[a-z0-9][-a-z0-9]*[a-z0-9]|[a-z0-9])"
                + "(?:\\.com\\.cn|\\.com)$)");
        for (String s : t) {
            Matcher m = p.matcher(s);
            if (m.find()) {
                System.out.println("Found " + m.group(1) + " in " + s);
            } else {
                System.out.println("Not found in " + s);
            }
        }

Open in new window

Looks a bit ugly because I couldn't find a more elegant way to enforce the "host name can't end or start with a hyphen" rule.

If you need to match more domains, not just ".com.cn" and ".com", then the second part should contain more complicated alternatives, but the idea stays the same.
0
 
LVL 1

Expert Comment

by:stephano12
Comment Utility
Try this code
public class TestSubstring {
public static void main(String[] args) {
	String[] string = {"www.abc.com","www.abc.com.cn","www.xyz.abc.com","www.xyz.abc.com.cn","xyz.abc.com","xyz.abc.com.cn"};
	for (String stg: string){
		System.out.println(stg.substring(0, stg.indexOf(".abc.com")));
	}

}
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
        String [] hosts = {
"www.abc.com",
"www.abc.com.cn",
"www.xyz.abc.com",
"www.xyz.abc.com.cn",
"xyz.abc.com",
"xyz.abc.com.cn"
};

        for(String sh : hosts){

            sh = sh.replaceAll(".*\\.(.+?\\.com)","$1");
            System.out.println("result: " + sh);

        }

Open in new window


Output:

result: abc.com
result: abc.com.cn
result: abc.com
result: abc.com.cn
result: abc.com
result: abc.com.cn

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 125 total points
Comment Utility
maybe you want even this way:

  
        String [] hosts = {
"tyr.mnm.org",                
"www.abc.com",
"www.abc.com.cn",
"www.xyz.abc.com",
"www.xyz.abc.com.cn",
"xyz.abc.com",
"xyz.abc.com.cn",
"xryz.ttt.net",
"www.dfg.com",
"tt.mju.edu",
"ttty.mki.edu.au"
};

        for(String sh : hosts){

            sh = sh.replaceAll("(?:.*\\.(.+?\\.com))|(?:.*\\.(.+?\\.net))|(?:.*\\.(.+?\\.org))|(?:.*\\.(.+?\\.edu))","$1$2$3$4");

            System.out.println("result: " + sh);

        }

Open in new window

result: mnm.org
result: abc.com
result: abc.com.cn
result: abc.com
result: abc.com.cn
result: abc.com
result: abc.com.cn
result: ttt.net
result: dfg.com
result: mju.edu
result: mki.edu.au

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:stachenov
Comment Utility
@for_yan, this doesn't work for something like "com.cn.com.cn".
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility

 why?  it returns:
result: cn.com.cn

com.cn.com

returns

cn.com

that is what is expected, as I understand.

And certainly for any regex you  can invent
some strign which will break it.


0
 
LVL 4

Expert Comment

by:stachenov
Comment Utility
Sorry, I was wrong, it actually works.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
No problem.
Though nothing is ideal, I'm sure there is some string which will break it.
Still it helps in great majority of cases
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Personally i would use URL.getHost
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now