public static String extractUrl(String content){
try {
String regex ="[(http(s)?):\\/\\/(www\\.)?a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)";
Pattern p = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(content);
if (m.find()) {
return m.group();
}
return "";
} catch (Exception e) {
return "";
}
}
.group(); 입력시 첫번째 url을 가져온다.
//Url만 있는지 없는지 확인, Url만 true, Url + 기타는 false 20221027 LHJ
public static boolean CheckOnlyUrl(final ChatVO value){
boolean checkOnlyUrl = false;
String requestUrl = value.getChatContent();
//Url 뽑아내는 작업
try {
String REGEX = "[(http(s)?):\\/\\/(www\\.)?a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)";
Pattern p = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(requestUrl);
if (m.find()) {
String host = m.group();
requestUrl = requestUrl.replaceAll(host, "").trim();
}
} catch (Exception e) {
e.printStackTrace();
}
//Emoji Check
Pattern rex = Pattern.compile("[\\x{10000}-\\x{10ffff}\ud800-\udfff]");
//Emoji Check and remove
Matcher rexMatcher = rex.matcher(requestUrl);
if(rexMatcher.find()){
requestUrl = EmojiParser.removeAllEmojis(requestUrl).trim();
}
if(requestUrl.length() > 0){
checkOnlyUrl = false;
}else {
checkOnlyUrl = true;
}
return checkOnlyUrl;
}
url을 가져와 유무를 체크하고
url이 있으면 true
url이 없으면 false을 리턴
참조 :
regex
https://story.dkserver.wo.tc/entry/JAVA-Android%EC%97%90%EC%84%9C-%EB%AC%B8%EC%9E%90%EC%97%B4%EC%97%90%EC%84%9C-URL%EC%B6%94%EC%B6%9C%ED%95%98%EA%B8%B0-%EC%A0%95%EA%B7%9C%EC%8B%9D
코드
https://jizard.tistory.com/237