正则表达式30分钟入门系列之2

2025-05-23 20:00:31

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; }}

正则表达式30分钟入门系列之2

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

正则表达式30分钟入门系列之2

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

正则表达式30分钟入门系列之2

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

正则表达式30分钟入门系列之2
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢