나는 특정 도메인과 하위 도메인을 필터링해야하는 url 목록이 있습니다. 내가 몇 가지 도메인을 가지고 있다고 말해.하위 도메인이 동일한 도메인의 자바를 사용하는지 확인하는 방법
http://www.example.com
http://test.example.com
http://test2.example.com
도메인 example.com에서 URL을 추출해야합니다.
나는 특정 도메인과 하위 도메인을 필터링해야하는 url 목록이 있습니다. 내가 몇 가지 도메인을 가지고 있다고 말해.하위 도메인이 동일한 도메인의 자바를 사용하는지 확인하는 방법
http://www.example.com
http://test.example.com
http://test2.example.com
도메인 example.com에서 URL을 추출해야합니다.
중첩 된 도메인이있는 경우에도 두 개의 URL이 동일한 하위 도메인에 속하는지 확인해야하는 프로젝트 작업 중. 위 가이드에서 수정했습니다. 지금까지 비슷한 결과를 얻었습니다 :
public static boolean isOneSubdomainOfTheOther(String a, String b) {
try {
URL first = new URL(a);
String firstHost = first.getHost();
firstHost = firstHost.startsWith("www.") ? firstHost.substring(4) : firstHost;
URL second = new URL(b);
String secondHost = second.getHost();
secondHost = secondHost.startsWith("www.") ? secondHost.substring(4) : secondHost;
/*
Test if one is a substring of the other
*/
if (firstHost.contains(secondHost) || secondHost.contains(firstHost)) {
String[] firstPieces = firstHost.split("\\.");
String[] secondPieces = secondHost.split("\\.");
String[] longerHost = {""};
String[] shorterHost = {""};
if (firstPieces.length >= secondPieces.length) {
longerHost = firstPieces;
shorterHost = secondPieces;
} else {
longerHost = secondPieces;
shorterHost = firstPieces;
}
//int longLength = longURL.length;
int minLength = shorterHost.length;
int i = 1;
/*
Compare from the tail of both host and work backwards
*/
while (minLength > 0) {
String tail1 = longerHost[longerHost.length - i];
String tail2 = shorterHost[shorterHost.length - i];
if (tail1.equalsIgnoreCase(tail2)) {
//move up one place to the left
minLength--;
} else {
//domains do not match
return false;
}
i++;
}
if (minLength == 0) //shorter host exhausted. Is a sub domain
return true;
}
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
return false;
}
그림 나는 비슷한 문제를 나중에 참조 할 수 있도록 남겨 두었습니다.
URL 클래스 등을 사용하여 멋진 솔루션을 찾고 있다고 생각하지만 필수는 아닙니다. 각 URL에서 "example.com"을 추출하는 방법을 생각하면됩니다.
참고 : example.com은 본질적으로 example.net과 다른 도메인입니다. 따라서 단지 "예"를 추출하는 것은 기술적으로 잘못된 행동입니다. 우리는 샘플 URL을 나눌 수
말 :
http://sub.example.com/page1.html
1 단계 : "/" 도메인을 포함하는 부분을 추출 구분 와 URL을 분할합니다.
이러한 각 부분
다음 블록의 형태에보고 될 수있다 (비어 있음)[www][subdomain][basedomain]
2 단계 : 폐기 "WWW" (있는 경우). 우리는 남아 있습니다 [하위] [basedomain]
3 단계 : "." 분할 구분 와 문자열
4 단계 : 분할에서 생성 된 문자열의 총 수를 찾을 수 있습니다. 2 개의 문자열이있는 경우 둘 다 대상 도메인 (example 및 com)입니다. > = 3 개의 문자열이 있으면 마지막 3 개의 문자열을 가져옵니다. 마지막 문자열의 길이가 3이면 마지막 2 개의 문자열은 도메인 (example 및 com)을 구성합니다. 마지막 문자열의 길이가 2 인 경우, 최근 3 문자열 난이 트릭을 할해야한다고 생각 도메인 (예 : 공동 및 영국)
을 포함한다 (나는이 숙제 아니었다 기대합니다 : D)
//You may clean this method to make it more optimum/better
private String getRootDomain(String url){
String[] domainKeys = url.split("/")[2].split("\\.");
int length = domainKeys.length;
int dummy = domainKeys[0].equals("www")?1:0;
if(length-dummy == 2)
return domainKeys[length-2] + "." + domainKeys[length-1];
else{
if(domainKeys[length-1].length == 2) {
return domainKeys[length-3] + "." + domainKeys[length-2] + "." + domainKeys[length-1];
}
else{
return domainKeys[length-2] + "." + domainKeys[length-1];
}
}
}
을 확인해주세요. 아이디어는 훌륭하지만 "co"에 집중할 수는 없습니다. .com.au (호주)는 무엇입니까? 당신은 더 이상 그러한 모든 도메인을 가진 목록을 만들고'list.contains (domainKeys [domainKeys.length-2])? .....'를 실행하십시오. –
고마워요! 나는 com.au에 관해 몰랐다. 더 포괄적 인 목록을 작성하는 데 사용할 수있는 도메인 유형의 소스를 알고 있습니까? –
실제로 두 번째 수준 도메인 (예 : .ac.uk)이 여러 개 있습니다. 나는 전체 목록을 찾지 못했습니다. 나는 최선의 가능성이 어떤 식 으로든 그것을 점검하는 것일 것이라고 생각한다. 음, 하나의 가능성은 splitted 배열의 길이를 확인하는 것입니다. –
기본 문자열 조작 숙제 – alehro
@Ramesh 답변을 업데이트했습니다. –