正则表达式30分钟入门系列之2
1、\w:这个关键字代表 一个字母、数字或下划线先来看看测试的脚手架代码:Code:package chapter4稆糨孝汶;import java.util.Arrays;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Created by MyWorld on 2016/3/23. */public class RegexStudyDemo { public static void main(String[] args) { String regexStr = "Hello\\w!"; List<String> input = Arrays.asList("Hello了!", "Hello3!", "HelloY!", "Hello_!"); System.out.println(isMatch(input, regexStr)); } private static boolean isMatch(List<String> inputs, String regexStr) { boolean result = true; for (String input : inputs) { Pattern pattern = Pattern.compile(regexStr); Matcher matcher = pattern.matcher(input); if (matcher.matches()) { continue; } System.out.println(input + " is not match!"); result = false; } return result; }}

3、不少眼尖的tx已经发现了,上面的代码执行时,返回false是因为“Hello\\w!”不能匹配“Hello了!”即\w不能匹配汉字童话里都是骗人的,不是说好的,可以匹配汉字的吗。。。

5、执行下看看是否匹配Output:Hello-! is not match!false与预期一致。OK

7、执行下看看结果是不是不匹配Output:Hello33! is not match!Hello ! is not match!false与预期一致OK
