正则表达式30分钟入门系列之5
1、[^]:这个关键字匹配 圈定一个范围,只要不是这个范围内的字符都可以哦^:这个关键字匹配 以哪个字符串开头$:这个关键字匹配 以哪个字符串结束先来看看测试的脚手架沪枭诽纾代码:Code:package chapter4;import java.util.Arrays;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;public class RegexStudyDemo { public static void main(String[] args) { String regexStr = "Hello[^\\d]!"; List<String> input = Arrays.asList("Hello7!", "Hello汉!", "Hello !", "Hello !", "Hello\b!", "Hello\t!", "Hello\"!" , "Hello\r!", "Hello\f!", "Hello\n!", "Hello_!", "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(String.format("%s is not match %s!", regexStr, input)); result = false; } return result; }}

3、修改一下正则表达式匹配一下的字腿发滏呸符串"Hello7!" 匹配刚才Code:String regexStr = "Hello[^\\D]!"稆糨孝汶;;List<String> input = Arrays.asList("Hello7!");

5、现在来看看^的用法更改下代码Code:String regexStr = "^[\\d]Hello!";List<String> input = 帆歌达缒Arrays.asList("7Hello!","xHello!");

7、最后来看看$的用法更改下代码Code:String regexStr = "Hello![\\d]$";List<String> input = Arrays.asList("Hello!7","Hello!x");
