|
1 | 1 | package com.regex; |
2 | 2 |
|
3 | | -import java.util.regex.Matcher; |
4 | | -import java.util.regex.Pattern; |
5 | | - |
| 3 | +/** |
| 4 | + * @Author: lenovo |
| 5 | + * @Date: 2020/2/2 21:46 |
| 6 | + * @Description: |
| 7 | + * |
| 8 | + * 详情参见:https://www.cnblogs.com/expiator/p/12250598.html |
| 9 | + */ |
6 | 10 | public class RegexDemo { |
7 | | - public static void main(String args[]){ |
8 | | - String str="1983-07-27"; |
9 | | - String pat="\\d{4}-\\d{2}-\\d{2}"; |
10 | | - Pattern p=Pattern.compile(pat); //先声明模式 |
11 | | - Matcher m=p.matcher(str); //接着再匹配 |
12 | | - if(m.matches()) { |
13 | | - System.out.println("日期格式各法"); |
14 | | - }else { |
15 | | - System.out.println("日期格式不合法"); |
16 | 11 |
|
17 | | - } |
| 12 | + public static void regex1(){ |
| 13 | + String str="abcdddabc"; |
| 14 | + String regex=".*d+.*"; |
| 15 | + if (str.matches(regex)){ |
| 16 | + System.out.println(true); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + public static void regex2(){ |
| 21 | + String str="abcdefg"; |
| 22 | + String regex=".*efg"; |
| 23 | + if (str.matches(regex)){ |
| 24 | + System.out.println(true); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + public static void regex3(){ |
| 30 | + String str="a"; |
| 31 | + String regex="\\w"; |
| 32 | + if (str.matches(regex)) { |
| 33 | + System.out.println(true); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public static void regex4(){ |
| 38 | + String str="abdf1459"; |
| 39 | + String regex="[a-z]{4}[1-9]{4}"; |
| 40 | + if (str.matches(regex)) { |
| 41 | + System.out.println(true); |
| 42 | + } |
| 43 | + } |
| 44 | + |
18 | 45 |
|
| 46 | + public static void regex6(){ |
| 47 | + String str="catdog"; |
| 48 | + String regex="^cat$"; |
| 49 | + String regex2="^cat.*"; |
| 50 | + String regex3=".*dog$"; |
| 51 | + boolean isMatch1=str.matches(regex); |
| 52 | + boolean isMatch2=str.matches(regex2); |
| 53 | + boolean isMatch3=str.matches(regex3); |
| 54 | + System.out.println(isMatch1+","+isMatch2+","+isMatch3); |
| 55 | + } |
19 | 56 |
|
20 | | - } |
21 | 57 | } |
0 commit comments