正则表达式(Regular Expression)匹配

    xiaoxiao2022-07-13  141

    正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本。

    许多程序设计语言都支持利用正则表达式进行字符串操作。例如,在Perl中就内建了一个功能强大的正则表达式引擎。正则表达式这个概念最初是由Unix中的工具软件(例如sed和grep)普及开的。正则表达式通常缩写成“regex”,单数有regexp、regex,复数有regexps、regexes、regexen。

    题目描述:请实现一个函数用来匹配包括"和"的正则表达式,模式中的   '.'  表示任意一个字符,而''*"表示它前面的个字符可以出现任意次(包含0次)。在本题中,匹配是指所有字符串的所有字符匹配整个模式,例如,字符串"aaa"与模式''a.a''和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配。

     

    public class   Solution{

       public  boolean match(char [ ] str,char[ ]  pattern){

      String a =new String (str);//把字符数组转换成字符串,通过构造函数

       String    regex =new String(pattern);

       return  a.matches(regex);

        }

    }

    常用正则表达式如下:

     

    package 正则表达式; import java.util.regex.Pattern; import java.security.acl.Group; import java.util.regex.Matcher; import org.junit.Test; public class Regxtest { /** * 正则表达式的最常用范例 */ @Test public void test1() { System.out.println("Hello!I'm Laurence.May you have a good luck!"); } @Test public void test2() { String regx = "\\d{11}";// 在字符串中要表示一个\转义字符,就要用两个\\匹配11 位整数 String string = "12345678911"; System.out.println(string.matches(regx)); } /** * * * */ @Test public void test3() { String regx = "hello";// 匹配的字符串里面要包含hello String str = "hello"; System.out.println(str.matches(regx)); } @Test public void test4() { String regx = "[abcdefgh]ello";// 该规则表示匹配abcdefgh中的任意一个字母。一次只匹配一个字母 String str = "e"; System.out.println(str.matches(regx)); } @Test public void test5() { String regx = "[a-zA-Z0-9]ello";// 该规则表示进匹配 String str = "hello"; System.out.println(str.matches(regx)); str = "aello"; System.out.println(str.matches(regx)); str = "2ello"; System.out.println(str.matches(regx)); } @Test public void test6() { String regx = "[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]o"; String str = "hello"; System.out.println(str.matches(regx)); str = "aello"; System.out.println(str.matches(regx)); str = "2ello"; System.out.println(str.matches(regx)); } /** * +:匹配1-多次 */ @Test public void test7() { String regx = "[a-zA-Z0-9+XZZCV]+"; String str = "hello"; System.out.println(str.matches(regx)); str = "aello"; System.out.println(str.matches(regx)); str = "2ello"; System.out.println(str.matches(regx)); } /* * *匹配0-多次(一个字符没有也匹配) * * */ public void test8() { String regx = "[a-zA-Z0-9]*"; String str = "hello"; System.out.println(str.matches(regx)); str = "aello"; System.out.println(str.matches(regx)); str = "2ello"; System.out.println(str.matches(regx)); } /* * ? 匹配0-1次(没有是可以的) * * */ public void test9() { String regx = "[a-zA-Z0-9]?"; String str = "hello"; System.out.println(str.matches(regx)); str = "aello"; System.out.println(str.matches(regx)); str = ""; System.out.println(str.matches(regx)); } /* * 匹配次数3次(超过3个或者小与3个都不匹配) */ @Test public void test10() { String regx = "[a-zA-Z0-9]{3}"; String str = "hello"; System.out.println(str.matches(regx)); str = "ael"; System.out.println(str.matches(regx)); str = "eo"; System.out.println(str.matches(regx)); } /** * 匹配次数3-5次 * * * */ @Test public void test11() { String regx = "[a-zA-Z0-9]{3,5}"; String str = "hello"; System.out.println(str.matches(regx)); str = "ael"; System.out.println(str.matches(regx)); str = "2e"; System.out.println(str.matches(regx)); } /* * 匹配3-多次 */ @Test public void test12() { String regx = "[a-zA-Z0-9]{3,}"; String str = "hello"; System.out.println(str.matches(regx)); str = "hel"; System.out.println(str.matches(regx)); str = "he"; System.out.println(str.matches(regx)); } @Test public void testy() { String regx = "[^0-9]{3}"; String str = "hello"; System.out.println(str.matches(regx)); str = "3el"; System.out.println(str.matches(regx)); str = "hel"; System.out.println(str.matches(regx)); } /** * * \\d匹配一个数字重复3次 \\D表示不匹配数字 * */ @Test public void test13() { String regx = "\\d{3}"; String str = "hello"; System.out.println(str.matches(regx)); str = "ael"; System.out.println(str.matches(regx)); str = "2eo"; System.out.println(str.matches(regx)); } /** * \\D不匹配数字 * * * */ @Test public void test14() { String regx = "\\D{3}"; String str = "hello"; System.out.println(str.matches(regx)); str = "ael"; System.out.println(str.matches(regx)); str = "2eo"; System.out.println(str.matches(regx)); } /** * * \\w匹配所有字符[a-zA-Z0-9] * */ @Test public void test15() { String regx = "\\w{3}"; String str = "hello"; System.out.println(str.matches(regx)); str = "ael"; System.out.println(str.matches(regx)); str = "2eo"; System.out.println(str.matches(regx)); } /** * 不匹配所有字符相当于[^a-zA-Z0-9] * */ @Test public void test16() { String regx = "\\W{3}"; String str = "hello"; System.out.println(str.matches(regx)); str = "@@@"; System.out.println(str.matches(regx)); str = "2eo"; System.out.println(str.matches(regx)); } /** * \\s匹配任何空白字符 \\S不匹配任何空白字符 * */ @Test public void test17() { String regx = "\\s{3}"; String str = " "; System.out.println(str.matches(regx)); str = " el"; System.out.println(str.matches(regx)); str = "2eo"; System.out.println(str.matches(regx)); } @Test public void test18() { String regx = "\\S{3}"; String str = "hello"; System.out.println(str.matches(regx)); str = " el"; System.out.println(str.matches(regx)); str = "2eo"; System.out.println(str.matches(regx)); } /** * .匹配任意字符 * * */ @Test public void test19() { String regx = ".{3}"; String str = "hel"; System.out.println(str.matches(regx)); str = "343"; System.out.println(str.matches(regx)); str = "2eo"; System.out.println(str.matches(regx)); } /* * \\代表一个\ * \/代表一个/ * \[代表一个[ * \]代表一个] * \.代表一个. * \{代表一个{ * \}代表一个} * \*匹配一个* * \+匹配一个+ * \?匹配一个? * 只要是特殊字符前面加一个/都可以匹配 */ @Test public void test20() { String regx = "\\\\"; String str = "\\"; System.out.println(str.matches(regx)); str = "\\"; System.out.println(str.matches(regx)); str = "\\"; System.out.println(str.matches(regx)); } @Test public void test21() { String regx = "\\\\"; String str = "\\"; System.out.println(str.matches(regx)); str = "\\"; System.out.println(str.matches(regx)); str = "\\"; System.out.println(str.matches(regx)); } @Test public void test22() { String regx ="...."; String str ="...."; System.out.println(str.matches(regx)); str = "...."; System.out.println(str.matches(regx)); str = "\\"; System.out.println(str.matches(regx)); } @Test /** * 分组和或 * */ public void test23() { String regx = "(\\(0\\d{2}\\)|0\\d{2})( |\\-|_)\\d{7,8}"; //(029)或029+空格或-加7-8个数字 String str = "019-4964135"; System.out.println(str.matches(regx)); str = "02934-4964135";// 超过次数就错误 System.out.println(str.matches(regx)); str = "(029)_78797967"; System.out.println(str.matches(regx)); str="(029) 4964135"; System.out.println(str.matches(regx)); } @Test public void test24() { Pattern pattern = Pattern.compile("(%)(.*?)(%)");// 模式对象 String str = "%vcdehcvd%
    转载请注明原文地址: https://yun.8miu.com/read-56939.html
    最新回复(0)