- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /*
- * 获取功能:获取下面字符串中由三个字符组成的单词
- * 例子: da jia ting woshuo jin tian yao xia yu ,bu shang wan zi xi ,gao xing bu?
- *
- */
- public class RegexDemo2 {
- public static void main(String[] args) {
- String s = "da jia ting woshuo jin tian yao xia yu ,bu shang wan zi xi ,gao xing bu?";
- // \\b字符边界
- String regex = "\\b\\w{3}\\b";
- // 把正则表达式编译成模式对象,传入regex
- Pattern p = Pattern.compile(regex);
- // 通过模式对象得到匹配器对象,传入s
- Matcher m = p.matcher(s);
- while (m.find()) {
- System.out.println(m.group());
- }
- }
- }
复制代码
|
|