The latest Java basic series of courses--Day05-string

Author Homepage: Programming Compass

About the author: High-quality creator in the Java field, CSDN blog expert
, CSDN content partner, invited author of Nuggets, Alibaba Cloud blog expert, 51CTO invited author, many years of architect design experience, resident lecturer of Tencent classroom

Main content: Java project, Python project, front-end project, artificial intelligence and big data, resume template, learning materials, interview question bank, technical mutual assistance

Favorites, likes, don't get lost, it's good to follow the author

Get the source code at the end of the article

day03 - string

1. Today's content introduction

There are many application scenarios of strings, and it can be said that they are everywhere.

For example, when a user logs in, the user name and password need to be verified, where the user name and password are both String
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-Jrn04hMw-1689819314385)(assets/1662605214383.png)]

For another example, when chatting with netizens, the text entered is actually a string

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-Y3dyT5a9-1689819314387)(assets/image-20230720100926516.png)]

For another example, when searching on Baidu, the search keyword is also a string

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-cpszCl9o-1689819314389)(assets/1662605442842.png)]

Two, package

1. What is a package

Before learning about strings, we need to learn about packages. Because there are many classes officially provided by Java, in order to manage these classes by category, others put the written classes in different packages.

A package is actually similar to a folder, and multiple class files can be placed in a package. As shown below

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-rf2HnpF3-1689819314390)(assets/1662605881879.png)]

The grammatical format of building a package:

//类文件的第一行定义包
package com.itheima.javabean;

public class 类名{
    
    
    
}

2. In your own program, call programs in other packages, you need to pay attention to the following problem

  • If the current program wants to call other programs under its own package, it can be called directly. (Classes under the same package can call each other directly)

  • If you want to call programs under other packages in the current program, you must import the package in the current program before you can access it!

    Import package format: import 包名.类名

  • If you want to call the program under the Java.lang package in the current program, you don't need to import the package, you can use it directly.

  • If the current program calls multiple programs under different packages, and the names of these programs happen to be the same, only one program can be imported by default, and the other program must be accessed with the package name.

3. String class

1. Overview of the String class

Dear students, next we will learn the String class, that is, we will learn to process strings. Why learn string processing? Because the processing of strings in development is still very common.

For example: when the user logs in, the user name and password entered by the user are sent to the background, and need to be verified with the correct user name and password, which requires the comparison function provided by the String class.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-MCwNPrTP-1689819314392)(assets/1662605347797.png)]

Another example: when students leave a message on the live broadcast, some friends may say some bad words in an uncivilized manner. The background detects that the bad words you entered are blocked by using swear words ***. This also requires the replacement function provided by the String class

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-m2ZNFP2S-1689819314393)(assets/1662605396550.png)]

In order to facilitate our processing of strings, Java provides us with a String class to represent strings, and this class is java.langunder the package.

According to the idea of ​​object-oriented programming, for string operations, you only need to create a string object, encapsulate the string data with the string object, and then call the method of the String class.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-BlWhlQBg-1689819314394)(assets/1662607669465.png)]


2. Create object from String

Next, let's open the API of the String class to see how objects of the String class are created. As shown below

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-GwE4acXc-1689819314395)(assets/1662607801186.png)]

In the API of the String class, there is such a sentence: "All string literals (such as "abc") in Java programs are instance implementations of strings." The instance implementation mentioned here actually refers to the string object.

This means that all Java string literals are string objects.

  • So the first way to create a String object is
String s1 = "abc"; //这里"abc"就是一个字符串对象,用s1变量接收

String s2 = "数字技术产业学院"; //这里的“数字技术产业学院”也是一个字符串对象,用s2变量接收
  • There is a second way to create a String object, which is to use the constructor of the String class to create an object of the String class.

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-DJlcxGwI-1689819314397)(assets/1662608166502.png)]

We have learned the construction method of the class before, and the new keyword is needed to execute the construction method. new String(参数)It is executing the constructor of the String class.

Below we demonstrate how to create an object of the String class through the constructor of the String class

// 1、直接双引号得到字符串对象,封装字符串数据
String name = "西亚斯666";
System.out.println(name);

// 2、new String创建字符串对象,并调用构造器初始化字符串
String rs1 = new String();
System.out.println(rs1); // ""

String rs2 = new String("xiyasi");
System.out.println(rs2);

char[] chars = {
    
    '西', '亚', '斯'};
String rs3 = new String(chars);
System.out.println(rs3);

byte[] bytes = {
    
    97, 98, 99};
String rs4 = new String(bytes);
System.out.println(rs4);

We will learn about what the String class is used for and the creation of String class objects. Finally to sum up

1. String是什么,可以做什么?
	答:String代表字符串,可以用来创建对象封装字符串数据,并对其进行处理。

2.String类创建对象封装字符串数据的方式有几种?
	方式一: 直接使用双引号“...” 。
	方式二:new String类,调用构造器初始化字符串对象。

3. Common methods of the String class

Dear students, in the last class, we learned how to encapsulate data through string objects. Next, we learn to call methods of the String class to process object string data.

Here we have picked out the common methods of the String class for the students, let's get to know them quickly. Why a quick acquaintance? Because the real role of API is to solve business needs, if you don't solve business needs, it is difficult to remember just remembering API.

![[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-2S2IRgDV-1689819314398)(assets/1662609378727.png)](https://img-blog.csdnimg.cn/54167b9db8a3415ca81384b95c518a2a. png)

Therefore, the correct way to open the API is to find this class first, quickly go through the methods in this class with code, and get a general impression. Then in a specific case, just choose the method you need to use.

Next, let's quickly go through the methods in the String class according to the calling rules of the methods. (Note: The first time you call the API method, you are calling it by looking at the API method, not memorizing it)

public class StringDemo2 {
    
    
    public static void main(String[] args) {
    
    
        //目标:快速熟悉String提供的处理字符串的常用方法。
        String s = "我爱Java";
        // 1、获取字符串的长度
        System.out.println(s.length());

        // 2、提取字符串中某个索引位置处的字符
        char c = s.charAt(1);
        System.out.println(c);

        // 字符串的遍历
        for (int i = 0; i < s.length(); i++) {
    
    
            // i = 0 1 2 3 4 5
            char ch = s.charAt(i);
            System.out.println(ch);
        }

        System.out.println("-------------------");

        // 3、把字符串转换成字符数组,再进行遍历
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
    
    
            System.out.println(chars[i]);
        }

        // 4、判断字符串内容,内容一样就返回true
        String s1 = new String("天马");
        String s2 = new String("天马");
        System.out.println(s1 == s2); // false
        System.out.println(s1.equals(s2)); // true

        // 5、忽略大小写比较字符串内容
        String c1 = "34AeFG";
        String c2 = "34aEfg";
        System.out.println(c1.equals(c2)); // false
        System.out.println(c1.equalsIgnoreCase(c2)); // true

        // 6、截取字符串内容 (包前不包后的)
        String s3 = "Java是最好的编程语言之一";
        String rs = s3.substring(0, 8);
        System.out.println(rs);

        // 7、从当前索引位置一直截取到字符串的末尾
        String rs2 = s3.substring(5);
        System.out.println(rs2);

        // 8、把字符串中的某个内容替换成新内容,并返回新的字符串对象给我们
        String info = "这个电影简直是个垃圾,垃圾电影!!";
        String rs3 = info.replace("垃圾", "**");
        System.out.println(rs3);

        // 9、判断字符串中是否包含某个关键字
        String info2 = "Java是最好的编程语言之一,我爱Java,Java不爱我!";
        System.out.println(info2.contains("Java"));
        System.out.println(info2.contains("java"));
        System.out.println(info2.contains("Java2"));

        // 10、判断字符串是否以某个字符串开头。
        String rs4 = "张三丰";
        System.out.println(rs4.startsWith("张"));
        System.out.println(rs4.startsWith("张三"));
        System.out.println(rs4.startsWith("张三2"));

        // 11、把字符串按照某个指定内容分割成多个字符串,放到一个字符串数组中返回给我们
        String rs5 = "张无忌,周芷若,殷素素,赵敏";
        String[] names = rs5.split(",");
        for (int i = 0; i < names.length; i++) {
    
    
            System.out.println(names[i]);
        }
    }
}

After demonstrating these methods of the String class, we already have a general impression of what methods strings have. At least know what String strings can do.

As for whether these methods of the String class are all memorized, this still needs to go through some case training. In the process of using it, find which method can solve your actual needs, and use which method. If the same method is used multiple times, it will naturally be remembered.

4.StringBuffer class

The StringBuffer class is a variable string class, and the content of the string can be modified at will after the object of the StringBuffer class is created. Each object of the StringBuffer class can store a string with a specified capacity. If the length of the string exceeds the capacity of the object of the StringBuffer class, the capacity of the object will be automatically expanded.

1. Create a StringBuffer class

The StringBuffer class provides 3 constructors to create a string, as follows:

  • StringBuffer() Constructs an empty string buffer, initialized to a capacity of 16 characters.
  • StringBuffer(int length) Create an empty string buffer and initialize it to the capacity of the specified length length.
  • StringBuffer(String str) Create a string buffer and initialize its content to the specified string content str, the initial capacity of the string buffer is 16 plus the length of the string str
// 定义一个空的字符串缓冲区,含有16个字符的容量
StringBuffer str1 = new StringBuffer();

// 定义一个含有10个字符容量的字符串缓冲区
StringBuffer str2 = new StringBuffer(10);

// 定义一个含有(16+4)的字符串缓冲区,"青春无悔"为4个字符
StringBuffer str3 = new StringBuffer("青春无悔");
/*
*输出字符串的容量大小
*capacity()方法返回字符串的容量大小
*/
System.out.println(str1.capacity());    // 输出 16
System.out.println(str2.capacity());    // 输出 10
System.out.println(str3.capacity());    // 输出 20

2. Append a string

The append() method of the StringBuffer class is used to append a string to the original StringBuffer object. The syntax of this method is as follows

StringBuffer对象.append(String str)

The function of this method is to append content to the end of the current StringBuffer object, similar to the concatenation of strings. After calling this method, the content of the StringBuffer object has also changed, for example:

StringBuffer buffer = new StringBuffer("hello,");    // 创建一个 StringBuffer 对象
String str = "World!";
buffer.append(str);    // 向 StringBuffer 对象追加 str 字符串
System.out.println(buffer.substring(0));    // 输出:Hello,World!

3. Replace the string

The setCharAt() method of the StringBuffer class is used to replace a character at the specified index position in the string. The syntax of this method is as follows:

StringBuffer对象.setCharAt(int index, char ch);

The function of this method is to modify the character whose index value is index in the object to a new character ch, for example:

StringBuffer sb = new StringBuffer("hello");
sb.setCharAt(1,'E');
System.out.println(sb);    // 输出:hEllo
sb.setCharAt(0,'H');
System.out.println(sb);    // 输出:HEllo
sb.setCharAt(2,'p');
System.out.println(sb);    // 输出:HEplo

4. Reverse the string

The reverse() method in the StringBuffer class is used to replace a string sequence with its reversed form. The syntax of this method is as follows:

StringBuffer 对象.reverse();
StringBuffer sb = new StringBuffer("java");
sb.reverse();
System.out.println(sb);    // 输出:avaj

5. Delete the string

The StringBuffer class provides deleteCharAt() and delete() two methods to delete strings, which are described in detail below.

  1. deleteCharAt() method

The deleteCharAt() method is used to remove the character at the specified position in the sequence. The syntax of this method is as follows:

StringBuffer 对象.deleteCharAt(int index);

The function of the deleteCharAt() method is to delete the character at the specified position, and then form the remaining content into a new string. For example:

StringBuffer sb = new StringBuffer("She");sb.deleteCharAt(2);System.out.println(sb);    // 输出:Sh

Execute this piece of code, delete the character whose index value is 2 in the string sb, and form a new string with the remaining content, so the value of the object sb is Sh.

  1. delete() method

The delete() method is used to remove the characters of the substring in the sequence. The syntax of this method is as follows:

StringBuffer 对象.delete(int start,int end);

Among them, start indicates the starting index value of the character to be deleted (including the character corresponding to the index value), and end indicates the end index value of the character string to be deleted (excluding the character corresponding to the index value). The function of this method is to delete all characters within the specified area, for example:

StringBuffer sb = new StringBuffer("hello jack");
sb.delete(2,5);
System.out.println(sb);    // 输出:he 
jacksb.delete(2,5);
System.out.println(sb);    // 输出:heck

Execute this piece of code to delete all characters from the string "hello jack" whose index value is 2 (inclusive) to index value 5 (exclusive), so the value of the new output string is "he jack".

The difference between String and StringBuffer:

StringStringBuffer的区别简单地说,就是一个变量和常量的关系。StringBuffer对象的内容可以修改;String对象一旦产生后就不可以被修改,重新赋值其实是两个对象。

1.StringBuffer:StringBuffer的内部实现方式和String不同,StringBuffer在进行字符串处理时,不生成新的对象,在内存使用上要优于String类。所以在实际使用时,如果经常需要对一个字符串进行修改,例如插入、删除等操作,使用StringBuffer要更加适合一些。

2.String:String类中没有用来改变已有字符串中的某个字符的方法,由于不能改变一个java字符串中的某个单独字符,所以在JDK文档中称String类的对象是不可改变的。然而,不可改变的字符串具有一个很大的优点:编译器可以把字符串设为共享的。

4. Regular expressions

1-character

Regular expression (regular expression, abbreviated as regex) is a powerful string (also known as character sequence) processing tool.

Using regular expressions can achieve the following goals

  1. Whether the given string meets the filter condition or match of the regular expression.

2. The specific part we want can be obtained from the string through regular expressions.

A regular expression is a string composed of ordinary characters (such as characters a to z) and "metacharacters", which are characters with special meanings.

character illustrate
\ Mark the next character as a special character, literal, backreference, or octal escape. For example, n matches the character n . \n matches a newline character. The sequence \\ matches \ , \( matches ( .
^ Matches where the input string begins. If the Multiline property of the RegExp object is set , ^ also matches the position after "\n" or "\r".
$ Matches the position at the end of the input string. If the Multiline property of the RegExp object is set , $ also matches the position before "\n" or "\r".
* Matches the preceding character or subexpression zero or more times. For example, zo* matches "z" and "zoo". * is equivalent to {0,}.
+ Matches the preceding character or subexpression one or more times. For example, "zo+" matches "zo" and "zoo", but not "z". + is equivalent to {1,}.
? Matches the preceding character or subexpression zero or one time. For example, "do(es)?" matches "do" in "do" or "does". ? is equivalent to {0,1}.
{ n} n is a non-negative integer. matches exactly n times. For example, "o{2}" does not match the "o" in "Bob", but matches two "o"s in "food".
{ n,} n is a non-negative integer. Match at least n times. For example, "o{2,}" does not match the "o" in "Bob", but matches all o's in "foooood". "o{1,}" is equivalent to "o+". "o{0,}" is equivalent to "o*".
{ n,m} m and n are non-negative integers, where n <= m . Matches at least n and at most m times. For example, "o{1,3}" matches the first three o's in "fooooood". 'o{0,1}' is equivalent to 'o?'. Note: You cannot insert spaces between commas and numbers.
? When this character immediately follows any other qualifier (*, +, ?, { n }, { n ,}, { n , m }), the matching pattern is "non-greedy". The "non-greedy" pattern matches the shortest possible string found, while the default "greedy" pattern matches the longest possible string found. For example, in the string "oooo", "o+?" matches only a single "o", while "o+" matches all "o".
. Matches any single character except "\r\n". To match any character including "\r\n", use a pattern such as "[\s\S]".
(pattern) Matches pattern and captures subexpressions of that match. Captured matches can be retrieved from the resulting "match" collection using the $0...$9 properties. To match bracket characters ( ), use "(" or ")".
(?:pattern) Matches pattern but does not capture a subexpression of that match, i.e. it is a non-capturing match and the match is not stored for later use. This is useful for combining pattern parts with the "or" character (|). For example, 'industr(?:y|ies) is a more economical expression than 'industry|industries'.
(?=pattern) A subexpression that performs a lookahead search that matches the string at the beginning of the string matching pattern . It is a non-capturing match, that is, a match that cannot be captured for later use. For example, 'Windows (?=95|98|NT|2000)' matches "Windows" in "Windows 2000", but not "Windows" in "Windows 3.1". The lookahead does not occupy characters, that is, after a match occurs, the search for the next match follows the previous match, not after the characters that make up the lookahead.
(?!pattern) A subexpression that performs a lookahead lookahead search that matches a search string that is not at the beginning of a string matching pattern . It is a non-capturing match, that is, a match that cannot be captured for later use. For example, 'Windows (?!95|98|NT|2000)' matches "Windows" in "Windows 3.1", but not "Windows" in "Windows 2000". The lookahead does not occupy characters, that is, after a match occurs, the search for the next match follows the previous match, not after the characters that make up the lookahead.
x|y Match x or y . For example, 'z|food' matches "z" or "food". '(z|f)ood' matches "zood" or "food".
[xyz] character set. Matches any of the contained characters. For example, "[abc]" matches "a" in "plain".
[^xyz] Reverse character set. Matches any character not contained. For example, "[^abc]" matches "p", "l", "i", "n" in "plain".
[a-z] range of characters. Matches any character in the specified range. For example, "[az]" matches any lowercase letter in the range "a" through "z".
[^a-z] Reverse range characters. Matches any character not in the specified range. For example, "[^az]" matches any character not in the range "a" through "z".
\b Matches a word boundary, the position between a word and a space. For example, "er\b" matches "er" in "never", but not "er" in "verb".
\B Matches on non-word boundaries. "er\B" matches "er" in "verb", but not "er" in "never".
\cx Matches the control character indicated by x . For example, \cM matches Control-M or carriage return. The value of x must be between AZ or az. If not, c is assumed to be the "c" character itself.
\d Numeric characters match. Equivalent to [0-9].
\D Matches non-numeric characters. Equivalent to [^0-9].
\f Form feed matches. Equivalent to \x0c and \cL.
\n Newline matches. Equivalent to \x0a and \cJ.
\r Matches a carriage return. Equivalent to \x0d and \cM.
\s Matches any whitespace character, including spaces, tabs, form feeds, etc. Equivalent to [ \f\n\r\t\v].
\S Matches any non-whitespace character. Equivalent to [^ \f\n\r\t\v].
\t Tab matching. Equivalent to \x09 and \cI.
\v Vertical tab matching. Equivalent to \x0b and \cK.
\w Matches any literal character, including underscore. Equivalent to "[A-Za-z0-9_]".
\W Matches any non-word character. Equivalent to "[^A-Za-z0-9_]".
\xn 匹配 n,此处的 n 是一个十六进制转义码。十六进制转义码必须正好是两位数长。例如,“\x41"匹配"A”。“\x041"与”\x04"&"1"等效。允许在正则表达式中使用 ASCII 代码。
*num* 匹配 num,此处的 num 是一个正整数。到捕获匹配的反向引用。例如,"(.)\1"匹配两个连续的相同字符。
*n* 标识一个八进制转义码或反向引用。如果 *n* 前面至少有 n 个捕获子表达式,那么 n 是反向引用。否则,如果 n 是八进制数 (0-7),那么 n 是八进制转义码。
*nm* 标识一个八进制转义码或反向引用。如果 *nm* 前面至少有 nm 个捕获子表达式,那么 nm 是反向引用。如果 *nm* 前面至少有 n 个捕获,则 n 是反向引用,后面跟有字符 m。如果两种前面的情况都不存在,则 *nm* 匹配八进制值 nm,其中 nm 是八进制数字 (0-7)。
\nml n 是八进制数 (0-3),ml 是八进制数 (0-7) 时,匹配八进制转义码 nml
\un 匹配 n,其中 n 是以四位十六进制数表示的 Unicode 字符。例如,\u00A9 匹配版权符号 (©)。

根据 Java Language Specification 的要求,Java 源代码的字符串中的反斜线被解释为 Unicode 转义或其他字符转义。因此必须在字符串字面值中使用两个反斜线,表示正则表达式受到保护,不被 Java 字节码编译器解释。例如,当解释为正则表达式时,字符串字面值 “\b” 与单个退格字符匹配,而 “\b” 与单词边界匹配。字符串字面值 “(hello)” 是非法的,将导致编译时错误;要与字符串 (hello) 匹配,必须使用字符串字面值 “\(hello\)”。

2 常用类的方法

正则表达式既可以直接用于字符串进行模式匹配,也可以利用Pattern和Matcher两个类使用正则表达式.

Java的正则表达式是由java.util.regex的Pattern和Matcher类实现的。Pattern对象表示经编译的正则表达式。静态的compile( )方法负责将表示正则表达式的字符串编译成Pattern对象。

1.matches( )

boolean flag = str.matches(regex);

可以快速判断能否在str中找到regex。

2.split( )

String[ ]  ss = s.split(regex);

用regex把字符串分隔开来,返回String数组。

3.find( )

while(matcher.find(i)){

i++;

}

Matcher.find( )的功能是发现CharSequence里的,与pattern相匹配的多个字符序列。

4.group

A(B(C))D 里面有三个组:

group(0) 是 ABCD

group(1) 是 BC

group(2) 是 C

形式为 matcher.group( )

5.start( )和end( )

如果匹配成功,start( )会返回此次匹配的开始位置,end( )会返回此次匹配的结束位置,即最后一个字符的下标加一。

如果之前的匹配不成功(或者没匹配),那么无论是调用start( )还是end( ),都会引发一 个IllegalStateException。

matcher.start( );

matcher.end( );

6.replace替换

replaceFirst(String replacement)将字符串里,第一个与模式相匹配的子串替换成replacement。

replaceAll(String replacement),将输入字符串里所有与模式相匹配的子串全部替换成replacement。

String result = s.replaceAll(regex,ss);

String result = s.replaceFirst(regex,ss);

7.reset( )

 用reset( )方法可以给现有的Matcher对象配上个新的CharSequence。

如果不给参数,reset( )会把Matcher设到当前字符串的开始处。

m.reset("java");

正则表达式在Java中的应用案例

1.判断功能
例:判断输入的手机号是否为13或者18开头

package zuoye2;
import java.util.Scanner;
public class EXjava {
    
    

	public static void main(String[] args) {
    
    
		Scanner input = new Scanner(System.in);
		System.out.print("请输入手机号:");
		String str = input.nextLine();
		String regex = "1[38]\\d{9}";//用正则表达式定义手机号规则
		boolean flag = str.matches(regex);
		System.out.println("手机号是:"+flag);
		input.close();
	}

}

2. Segmentation function
Example: Segmentation of age groups

package zuoye2;
import java.util.Scanner;
public class EXjava {
    
    

	public static void main(String[] args) {
    
    
		Scanner input = new Scanner(System.in);
	 
	    String age = "18-30";//定义年龄范围
		String regex = "-";
		String[] strArr = age.split(regex);//分割成字符串数组
		
		int startage = Integer.parseInt(strArr[0]);
		int endage = Integer.parseInt(strArr[1]);
		
		System.out.print("请输入年龄:");
		int a = input.nextInt();
		if(a >= startage && a <= endage) {
    
    
			System.out.println("Yes");
		}
		else {
    
    
			System.out.println("No");
		}
		input.close();
	}

}

3. Replacement function
Example: Replace the number of the string with *

package zuoye2;
import java.util.Scanner;
public class EXjava {
    
    

	public static void main(String[] args) {
    
    
		Scanner input = new Scanner(System.in);
	 
	    String s = "12342javawang2345";
		String regex = "\\d";
		String ss = "*";
		//将字符串里面的数字替换成*
		String result = s.replaceAll(regex, ss);
		System.out.println(result);
		
		input.close();
	}


4. Use Pattern and Matcher for character matching

        String str = "13111225544";
        String regexp = "\\d{11}$";
        boolean b = Pattern.matches(regexp, str);
        System.out.println("正则匹配结果="+b);

        Pattern p = Pattern.compile(regexp);
        Matcher m = p.matcher(str);
        boolean b2 = m.matches();
        System.out.println("正则匹配结果="+b2);

       

Guess you like

Origin blog.csdn.net/whirlwind526/article/details/131824219