예상대로 작동하지 않는 코드가 있습니다. 아이디어는 url에서 슬래시를 사용하여 그룹을 일치시키는 것입니다. 슬래시의 수는 1 개 이상이어야합니다. 알고리즘은 슬래시의 수를 두 개로 만 바꿔야합니다. 코드를 수정하는 방법?
HttpURLConverter
public class HttpURLConverter {
final private String UrlPattern = "((([A-Za-z]{3,9}:(?:\\/\\/)?)(?:[\\-;:&=\\+\\$,\\w]+@)?[A-Za-z0-9\\.\\-]+|(?:www\\.|[\\-;:&=\\+\\$,\\w]+@)[A-Za-z0-9\\.\\-]+)((?:\\/[\\+~%\\/\\.\\w\\-_]*)?\\??(?:[\\-\\+=&;%@\\.\\w_]*)#?(?:[\\.\\!\\/\\\\\\w]*))?)";
URL validateURL(URL url) throws MalformedURLException {
URL validURL = null;
if(!Pattern.matches(UrlPattern, url.toString())){
if(Pattern.matches("(https?|ftp|file):.*", url.toString())){
Matcher matcher = Pattern.compile("(https?|ftp|file):(\\/)*([A-za-z0-9\\.\\-?#_]+)([A-za-z0-9\\.\\-?#_\\/]{0,})", Pattern.CASE_INSENSITIVE).matcher(url.toString());
List<String> allMatches = new ArrayList<String>();
while (matcher.find()) {
allMatches.add(matcher.group());
}
if(allMatches.size() > 1){
System.out.println(allMatches.get(2));
allMatches.set(2, "//"); // replace any number of slashes with only two
validURL = new URL(allMatches.toString());
}else{
throw new RuntimeException("Expected slashes after URL shema definition but found none.");
}
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
System.out.println(matcher.group(3));
System.out.println(matcher.group(4));
}else{
throw new RuntimeException("Given url is not valid. URL shema is not detected");
}
}
return validURL;
}
}
테스트
@Test
public void testHttpURLConverter2() throws MalformedURLException{
assertEquals("http://google.com", new HttpURLConverter().validateURL(new URL("http:///google.com")));
}
@Test
public void testHttpURLConverter2() throws MalformedURLException{
assertEquals("http://google.com", new HttpURLConverter().validateURL(new URL("http:/google.com")));
}
이것이 제가 생각해 낼 수있는 최적의 솔루션입니다. 100 % 성공 결과를 얻으려면 계속 확인하고 추가 처리해야합니다. 이 메소드는 두 테스트 입력 모두에 대해 검증 된 URL을 인쇄합니다.
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HttpURLConverter {
final private String UrlPattern = "((([A-Za-z]{3,9}:(?:\\/\\/)?)(?:[\\-;:&=\\+\\$,\\w]+@)?[A-Za-z0-9\\.\\-]+|(?:www\\.|[\\-;:&=\\+\\$,\\w]+@)[A-Za-z0-9\\.\\-]+)((?:\\/[\\+~%\\/\\.\\w\\-_]*)?\\??(?:[\\-\\+=&;%@\\.\\w_]*)#?(?:[\\.\\!\\/\\\\\\w]*))?)";
URL validateURL(URL url) throws MalformedURLException {
//System.out.println(url);
URL validURL = null;
if (!Pattern.matches(UrlPattern, url.toString())) {
if (Pattern.matches("(https?|ftp|file):.*", url.toString())) {
Matcher matcher = Pattern
.compile("(https?|ftp|file):(\\/)*([A-za-z0-9\\.\\-?#_]+)([A-za-z0-9\\.\\-?#_\\/]{0,})", Pattern.CASE_INSENSITIVE)
.matcher(url.toString());
List<String> allMatches = new ArrayList<String>();
while (matcher.find()) {
allMatches.add(matcher.group());
}
for (String str : allMatches) {
String regex = "(\\/)+";
str = str.replaceAll(regex, "//");
validURL = new URL(str);
System.out.println("Validated URL : " + validURL);
}
} else {
throw new RuntimeException("Given url is not valid. URL shema is not detected");
}
}
return validURL;
}
public static void main(String[] args) throws MalformedURLException {
new HttpURLConverter().validateURL(new URL("http:////google.com"));
}
}
다음과 같은 출력이 표시됩니다.
http:////google.com
Validated URL : http://google.com
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다