欢迎访问宙启技术站
智能推送

使用Java正则表达式的10个函数

发布时间:2023-06-09 04:23:46

正则表达式是一种强大的工具,可用于在文本中查找和替换模式。Java提供了许多正则表达式函数,使我们能够在应用程序中轻松使用它们。 在本文中,我们将介绍Java中的10个常用正则表达式函数。

1. String.matches()函数

matches()函数是用于检查字符串是否与指定的正则表达式相匹配。 该函数将返回布尔值true或false。 如果字符串匹配正则表达式,则返回true,否则返回false。

语法:

public boolean matches(String regex)

示例:

String str = "Hello World!";
boolean match = str.matches("^[Hh]ello\\s[A-Za-z]+!$");
System.out.println(match); //true

2. Pattern.compile()函数

compile()函数是将字符串编译为正则表达式模式。 编译后的模式可用于在其他字符串上执行匹配操作。 返回的模式对象可以多次使用。

语法:

public static Pattern compile(String regex)

示例:

String regex = "[0-9]+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("12345");
while (matcher.find()) {
    System.out.println(matcher.group());
}

3. Pattern.matcher()函数

matcher()函数是用于创建与指定正则表达式匹配的匹配器。 返回的Matcher对象可以用于执行各种不同的匹配操作。

语法:

public Matcher matcher(String input)

示例:

String regex = "\\d+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("12345 67890");
while (matcher.find()) {
    System.out.println(matcher.group());
}

4. Matcher.find()函数

find()函数是用于在输入字符串中查找与模式匹配的子序列。 如果找到了匹配的子序列,则将返回true,否则返回false。

语法:

public boolean find()

示例:

String regex = "\\d+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("12345 67890");
while (matcher.find()) {
    System.out.println(matcher.group());
}

5. Matcher.group()函数

group()函数是用于返回与前一个匹配操作匹配的子字符串。 可以使用int参数指定匹配的子组。 如果未指定参数,则将返回完整的匹配子字符串。

语法:

public String group([int group])

示例:

String regex = "(\\w+)\\s+(\\w+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("John Smith");
if (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
    System.out.println("First Group: " + matcher.group(1));
    System.out.println("Second Group: " + matcher.group(2));
}

6. Matcher.replaceAll()函数

replaceAll()函数是用于将输入字符串中所有与正则表达式相匹配的文本替换为指定的替换字符串。

语法:

public String replaceAll(String replacement)

示例:

String regex = "\\s+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("Hello World");
String replaced = matcher.replaceAll("-");
System.out.println(replaced); // Hello-World

7. Matcher.start()函数

start()函数是用于返回匹配的子字符串的起始索引。

语法:

public int start()
public int start(int group)

示例:

String regex = "John";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("My name is John");
if (matcher.find()) {
    System.out.println("Match start: " + matcher.start());
    System.out.println("Match end: " + matcher.end());
}

8. Matcher.end()函数

end()函数是用于返回匹配的子字符串的结束索引。

语法:

public int end()
public int end(int group)

示例:

String regex = "John";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("My name is John");
if (matcher.find()) {
    System.out.println("Match start: " + matcher.start());
    System.out.println("Match end: " + matcher.end());
}

9. Pattern.quote()函数

quote()函数是用于返回指定字符串的字面值,无需进行任何转义或特殊处理。

语法:

public static String quote(String s)

示例:

String regex = "(.*?)(\\d+)(.*)";
String input = "Some text 1234 more text";
String quotedRegex = Pattern.quote(regex);
Pattern pattern = Pattern.compile(quotedRegex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
}

10. Pattern.matches()函数

matches()函数是用于检查输入字符串是否与指定的正则表达式完全匹配。

语法:

public static boolean matches(String regex, CharSequence input)

示例:

String regex = "\\d+";
System.out.println(Pattern.matches(regex, "1234")); //true
System.out.println(Pattern.matches(regex, "1234abc")); //false

以上是Java正则表达式的10个常用函数,它们为开发人员提供了广泛的选择,以在应用程序中有效地使用正则表达式。 了解并掌握这些函数可以大大提高代码的效率和性能。