Android
코틀린 HTML 태그 제거하기.
일용직 코딩노동자
2023. 1. 3. 09:13
728x90
반응형
fun removeTag(str: String?): String? {
var str = str
str = Normalizer.normalize(str, Normalizer.Form.NFKC)
var mat: Matcher
// script 처리
val script = Pattern.compile("<(no)?script[^>]*>.*?</(no)?script>", Pattern.DOTALL)
mat = script.matcher(str)
str = mat.replaceAll("")
// style 처리
val style = Pattern.compile("<style[^>]*>.*</style>", Pattern.DOTALL)
mat = style.matcher(str)
str = mat.replaceAll("")
// tag 처리
val tag = Pattern.compile("<(\"[^\"]*\"|\'[^\']*\'|[^\'\">])*>")
mat = tag.matcher(str)
str = mat.replaceAll("")
// ntag 처리
val ntag = Pattern.compile("<\\w+\\s+[^<]*\\s*>")
mat = ntag.matcher(str)
str = mat.replaceAll("")
// entity ref 처리
val Eentity = Pattern.compile("&[^;]+;")
mat = Eentity.matcher(str)
str = mat.replaceAll("")
// whitespace 처리
val wspace = Pattern.compile("\\s\\s+")
mat = wspace.matcher(str)
str = mat.replaceAll("")
return str
}
해당 함수를 작성하시고 인자값으로 제거하고자 하는 문자열을 넣어주시면 태그만 제거되고 리턴값으로 나옵니다.
ㄱ ㅏ ㄱ 푸시
ㅗ o ㅗ 푸시 :D
728x90
반응형