> 文章列表 > 通过Java语言学正则表达式

通过Java语言学正则表达式

通过Java语言学正则表达式

1. 常用正则表达式

日期验证正则表达式


2. Java + Regular

2.1. 空白字符(White Space)替换为""

// 空白字符(White Space)替换为""
String str = " \\n\\r\\f\\t  a  \\n\\r\\f\\t b \\n\\r\\f\\t  c \\n\\r\\f\\t ";
str.replaceAll("\\\\s+", ""); //abc// 大写字符替换为空
str.replaceAll("[A-Z]", "") 
// 小写字母替换为空
str.replaceAll("[a-z]", "")
public String replaceAll(String regex, String replacement)
public String replace(char oldChar, char newChar)
public String replace(CharSequence target, CharSequence replacement)

2.2. 匹配""

参考:正则表达式如何匹配""

Pattern compile = Pattern.compile("^$");System.out.println(compile.matcher("").find());  //trueSystem.out.println(compile.matcher("").matches());  //trueSystem.out.println("".matches("^$"));  //true

2.3 Java Matcher对象中 find() 与 matches() 的区别

find():字符串某个部分匹配上正则表达式就会返回true
matches():整个字符串都匹配上正则表达式才返回true,否则false
参考:
Java Matcher对象中find()与matches()的区别
Pattern隐藏了哪些Java8追加的新功能
Matcher类有哪些我们必须掌握的方法?

Pattern compile = Pattern.compile("[WASD][0-9]{1,2}");
System.out.println(compile.matcher("A1").find());  //true
System.out.println(compile.matcher("A12").find());  //true
System.out.println(compile.matcher("E12").find());  //false
System.out.println(compile.matcher("AE12").find());  //false
System.out.println(compile.matcher("AEA12").find());  //true
System.out.println(compile.matcher("AEA1A2").find());  //true
System.out.println(compile.matcher("AEA12444").find());  //truePattern compile = Pattern.compile("[WASD][0-9]{1,2}");
System.out.println(compile.matcher("A122").matches());  //false
System.out.println(compile.matcher("AA12").matches());  //false
System.out.println(compile.matcher("A12").matches());  //true
System.out.println(compile.matcher("A1").matches());  //true
System.out.println(compile.matcher("AA1").matches());  //false
System.out.println(compile.matcher("AA12").matches());  //falseSystem.out.println("A122".matches("[WASD][0-9]{1,2}")); // false
System.out.println("AA12".matches("[WASD][0-9]{1,2}")); // false
System.out.println("A12".matches("[WASD][0-9]{1,2}")); // true
System.out.println("A1".matches("[WASD][0-9]{1,2}")); // true
System.out.println("A1".matches("[WASD]{1,2}[0-9]{1,2}")); // true
System.out.println("A12".matches("[WASD]{1,2}[0-9]{1,2}")); // true
System.out.println("AA1".matches("[WASD]{1,2}[0-9]{1,2}")); // true
System.out.println("AA12".matches("[WASD]{1,2}[0-9]{1,2}")); // true

2.4 断言是否包含

Predicate<String> predicate = Pattern.compile("dalian").asPredicate();System.out.println(predicate.test("welcome dalian")); //true
System.out.println(predicate.test("welcome dalian1")); //true
System.out.println(predicate.test("welcome dalia")); //false

2.5 按正则分割字符串

Pattern pattern = Pattern.compile("dalian");
Stream<String> stream = pattern.splitAsStream("Welcomedalianfordalian!");List<String> collect = stream.collect(Collectors.toList()); // [Welcome, for, !]

2.6 Group

group是针对()来说的,group(0)就是指的整个串,group(1) 指的是第一个括号里的东西,group(2)指的第二个括号里的东西。
参考:
Java正则表达式–Matcher.group函数的用法
matcher.group()方法的使用

Pattern compile = Pattern.compile("(count)(\\\\d+)(df)");
Matcher matcher = compile.matcher("count000dfdfsdffaaaa1");
if (matcher.find()) {System.out.println(matcher.group()); // count000dfSystem.out.println(matcher.group(0)); // count000dfSystem.out.println(matcher.group(1)); // countSystem.out.println(matcher.group(2)); // 000System.out.println(matcher.group(3)); // df
}

5. 算法

5.1 DEMO1

1、合法坐标为A(或者D或者W或者S) + 数字(两位以内)
2、坐标之间以;分隔。
3、非法坐标点需要进行丢弃。如AA10; A1A; %; YAD; 等。

List<String> actions = strs.stream().filter(a -> a != null && a.length() > 0).filter(word -> word.matches("[WASD][0-9]{1,2}")).collect(Collectors.toList());

5.2 包括大小写字母.数字.其它符号,以上四种至少三种

/* 包括大小写字母.数字.其它符号,以上四种至少三种*/
private static boolean getMatch(String str) {int count = 0;Pattern p1 = Pattern.compile("[A-Z]");if (p1.matcher(str).find()) {count++;}Pattern p2 = Pattern.compile("[a-z]");if (p2.matcher(str).find()) {count++;}Pattern p3 = Pattern.compile("[0-9]");if (p3.matcher(str).find()) {count++;}Pattern p4 = Pattern.compile("[^a-zA-Z0-9]");if (p4.matcher(str).find()) {count++;}if (count >= 3) {return false;} else {return true;}
}