정규식 "\\S" 공백이 아닌 문자와 일치하고 다음 정규식은 굵은 태그 사이에 있는 하나 이상의 공백이 아닌 문자와 일치합니다.
"(\\S+)"
따라서 HTML 스크립트에서 굵은 필드를 일치시키려면 다음을 수행해야 합니다. -
-
compile() 메서드를 사용하여 위의 정규식을 컴파일합니다.
-
matcher() 메서드를 사용하여 얻은 패턴에서 matcher를 검색합니다.
-
group() 메서드를 사용하여 입력 문자열의 일치하는 부분을 인쇄합니다.
예시
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
String str = "<p>This <b>is</b> an <b>example>/b> HTML <b>script</b>.</p>";
//Regular expression to match contents of the bold tags
String regex = "<b>(\\S+)</b>";
//Creating a pattern object
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(str);
//Creating an empty string buffer
while (matcher.find()) {
System.out.println(matcher.group());
}
}
} 출력
<b>is</b> <b>example</b> <b>script</b>