\b Java 정규식의 메타 문자는 단어 경계와 일치합니다. 따라서 주어진 입력 텍스트에서 특정 단어를 찾으려면 정규식의 단어 경계 내에서 필수 단어를 −
로 지정합니다."\\brequired word\\b";
예시 1
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MachingWordExample1 {
public static void main( String args[] ) {
//Reading string value
Scanner sc = new Scanner(System.in);
System.out.println("Enter input string");
String input = sc.next();
//Regular expression to find digits
String regex = "\\bhello\\b";
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex);
//Retrieving the matcher object
Matcher matcher = pattern.matcher(input);
if(matcher.find()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
}
} 출력
Enter input string hello welcome to Tutorialspoint Match found
예시 2
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherExample2 {
public static void main( String args[] ) {
String input = "This is sample text \n " + "This is second line " + "This is third line";
String regex = "\\bsecond\\b";
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex);
//Retrieving the matcher object
Matcher matcher = pattern.matcher(input);
if(matcher.find()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
}
} 출력
Match found