Java study notes 11 regular expressions

A regular expression defines the pattern of a string, which is essentially a special string object.

Regular expressions can also be used to search, edit or process text.

String supports regular expression methods

.replaceAll()

.replaceFirst()

.split()

.matches()

character illustrate example
^ expression start
$ end of expression
[358a-z] A symbol, the symbol range is 3,5,8,az
\d === [0-9] represents a digit \\d{9}
\d+ Represents 1 number, or multiple numbers Quite [0-9]{1,}
\d{3} Represents 3 digits Equivalent to [9-9]{3}
[\u4e00-\u9fa5]{2,8} Represents 2 to 8 Chinese characters
* Represents 0 or more previous symbols {0,} [a-z]*
. represents an arbitrary symbol str.matches(“^.+$”)
+ Represents 1 or more previous symbols {1,} \\d+
? Represents 0 or 1 previous symbols {0,1}
{n} n
{n,} n or more
{m,n} [a-zA-Z_0-9]{6,18} sum \w{6,18}
\D [^0-9]
\w [a-zA-Z0-9_]
\W [^a-zA-Z0-9_]
| match or regex or, grouping function
() group
\s Represents a space \s

In java, the start and end are automatically judged.

Using a regular expression, convert a number in a string to nothing.

		String str = "hello16java16mysql8html";
        System.out.println(str);
        //删除所有数字   \\d 代表数字   \\D 代表非数字(\\d取反)
        String s1 = str.replaceAll("\\d", "");
        System.out.println(s1);

The result of the operation is as follows
insert image description here

Using regex, remove anything that is not a number.

		String str = "hello16java16mysql8html";
        System.out.println(str);
        //删除不是数字的内容
        String s1 = str.replaceAll("\\D", "");
        System.out.println(s1);

The result of the operation is as follows
insert image description here

Delete the character string that appears for the first time to match the deletion, + means {1,} the number that appears for the first time, no matter how many can be deleted.

 String str = "hello16java16mysql8html";
        System.out.println(str);
        //删除首次出现的字符串匹配删除 \\d+
        String s1 = str.replaceFirst("\\d+", "");
        System.out.println(s1);

insert image description here

Determine whether a string is capitalized or not

String str = "hello16java16mysql8html";
        String str1 = "helGlo16jaAva16mysql8html";
        System.out.println(str);
        if (str.matches("^.*[A-Z].*$")) {
    
    
            System.out.println("有大写");
        } else {
    
    
            System.out.println("没大写");
        }


        System.out.println(str1);
        if (str1.matches("^.*[A-Z].*$")) {
    
    
            System.out.println("有大写");
        } else {
    
    
            System.out.println("没大写");
        }

insert image description here

Determine whether a string has numbers

 		String str = "hello16java16mysql8html";
        System.out.println(str);
        System.out.println(str.matches("^.*\\d.*$"));

insert image description here

Determine whether a string has uppercase or lowercase letters

		String str = "hello16java16mysql8html";
        System.out.println(str);
        System.out.println(str.matches("^.*\\w.*$"));

insert image description here

Determine whether the string has Chinese

Range of Chinese character strings

insert image description here

 		String str = "hello16java16mysql8html";
        System.out.println(str);
        System.out.println(str.matches("^.*[\u4e00-\u9fa5].*$"));

insert image description here

Determine the phone number

String phone = "13014577066";
        String pattern = "^1[3,5,8]\\d{9}$";
        if (phone.matches(pattern)) {
    
    
            System.out.println("yes");
        } else {
    
    
            System.out.printf("手机号:%s不是合法的手机号。 \n", phone);
        }

insert image description here

//是不是全英文
System.out.println("abc".matches("[a-zA-Z]+"));//true
System.out.println("ab c".matches("[a-zA-Z]+"));//false
System.out.println("ab123c".matches("[a-zA-Z]+"));//false
System.out.println("abDDDc".matches("[a-zA-Z]{1,}"));//true

greedy mode

String h = "<div>hello-17</div><div>hello</div><span>java</span><div>java</div>";
        System.out.println(h);
        Pattern p = Pattern.compile("(<div>)(.*?hello.*?)(</div>)");
        Matcher m = p.matcher(h);
        while (m.find()) {
    
    
            System.out.println(m.group(2));
        }

insert image description here

plus? It means to cancel the greedy mode. If it is not canceled, it will be calculated with the maximum length.

String h = "<div>hello-17</div><div>hello</div><span>java</span><div>java</div>";
        System.out.println(h);
        Pattern p = Pattern.compile("(<div>)(.*hello.*)(</div>)");
        Matcher m = p.matcher(h);
        while (m.find()) {
    
    
            System.out.println(m.group(2));
        }

insert image description here

Determine where a character occurs in a string

		String s = "java-ajavajavajavabc-mysqljavascript-aajava 16ajav";
        String d = "java";
        Pattern p = Pattern.compile("java");
        Matcher m = p.matcher(s);
        int nn = 0;
        while (m.find()) {
    
    
            ++nn;
            //System.out.printf("%s ", m.group());
        }
        System.out.printf("%s 在 %s 中出现了 %d 次。%n", d, s, nn);

        int n = s.split("java").length > 0 ? s.split("java").length - 1 : 0;
        if (s.endsWith("java")) {
    
    
            n += 1;
        }
        System.out.println(n);

insert image description here

Guess you like

Origin blog.csdn.net/xxxmou/article/details/129148976