在Java中的正则表达式 – Java的正则表达式示例

欢迎来到Java中的正则表达式。在Java中它也被称为Regex。当我开始编程时,Java正则表达式对我来说是一场噩梦。这个教程旨在帮助您掌握Java中的正则表达式。我也会回来这里复习我的Java Regex学习。

在Java中的正则表达式

在Java中的正则表达式定义了字符串的模式。正则表达式可用于搜索、编辑或操作文本。正则表达式并不特定于某种语言,但在每种语言中略有不同。Java中的正则表达式与Perl最为相似。Java正则表达式类位于java.util.regex包中,包含三个类。

    1. Pattern: Pattern对象是正则表达式的编译版本。Pattern类没有任何公共构造函数,我们使用它的公共静态方法compile通过传递正则表达式参数来创建Pattern对象。

 

    1. Matcher: Matcher是用于将输入字符串模式与创建的模式对象进行匹配的Java正则表达式引擎对象。Matcher类没有任何公共构造函数,我们使用模式对象的matcher方法来获取Matcher对象,该方法以输入字符串作为参数。然后我们使用matches方法,根据输入字符串是否与正则表达式模式匹配返回布尔结果。

 

    PatternSyntaxException: 如果正则表达式语法不正确,将抛出PatternSyntaxException异常。

让我们来看一下Java Regex的示例程序。

package com.Olivia.util;

import java.util.regex.*;

public class PatternExample {

	public static void main(String[] args) {
		Pattern pattern = Pattern.compile(".xx.");
		Matcher matcher = pattern.matcher("MxxY");
		System.out.println("Input String matches regex - "+matcher.matches());
		// bad regular expression
		pattern = Pattern.compile("*xx*");

	}

}

当我们运行这个Java正则表达式示例程序时,我们得到以下输出。

Input String matches regex - true
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
*xx*
^
	at java.util.regex.Pattern.error(Pattern.java:1924)
	at java.util.regex.Pattern.sequence(Pattern.java:2090)
	at java.util.regex.Pattern.expr(Pattern.java:1964)
	at java.util.regex.Pattern.compile(Pattern.java:1665)
	at java.util.regex.Pattern.(Pattern.java:1337)
	at java.util.regex.Pattern.compile(Pattern.java:1022)
	at com.Olivia.util.PatternExample.main(PatternExample.java:13)

由于Java正则表达式围绕字符串(String)展开,因此在Java 1.4中对String类进行了扩展,提供了一个名为matches的方法来进行正则表达式模式匹配。内部使用Pattern和Matcher java正则表达式类来进行处理,但显然它减少了代码行数。Pattern类还包含了一个matches方法,它接受正则表达式和输入字符串作为参数,并在匹配后返回布尔结果。因此,下面的代码在Java中使用正则表达式可以很好地工作。

String str = "bbb";
System.out.println("Using String matches method: "+str.matches(".bb"));
System.out.println("Using Pattern matches method: "+Pattern.matches(".bb", str));

如果您只需要检查输入字符串是否与模式匹配,您可以通过使用简单的字符串匹配方法来节省时间和代码行数。只有在您需要操作输入字符串或需要重复使用模式时,才应该使用Pattern和Matches类。请注意,正则表达式定义的模式从左到右应用于字符串,并且一旦源字符用于匹配,就无法重复使用。例如,正则表达式“121”将仅在“31212142121”中匹配两次,如“_121____121”。

Java中的正则表达式:常见匹配符号

Regular Expression Description Example
. Matches any single character (“…”, “a%”) – true(“…”, “.a”) – true (“…”, “a”) – false
^aaa Matches aaa regex at the beginning of the line (“^a.c.”, “abcd”) – true (“^a”, “ac”) – false
aaa$ Matches regex aaa at the end of the line (“…cd$”, “abcd”) – true(“a$”, “a”) – true (“a$”, “aca”) – false
[abc] Can match any of the letter a, b or c. [] are known as character classes. (“^[abc]d.”, “ad9”) – true(“[ab].d$”, “bad”) – true (“[ab]x”, “cx”) – false
[abc][12] Can match a, b or c followed by 1 or 2 (“[ab][12].”, “a2#”) – true(“[ab]…[12]”, “acd2”) – true (“[ab][12]”, “c2”) – false
[^abc] When ^ is the first character in [], it negates the pattern, matches anything except a, b or c (“[^ab][^12].”, “c3#”) – true(“[^ab]…[^12]”, “xcd3”) – true (“[^ab][^12]”, “c2”) – false
[a-e1-8] Matches ranges between a to e or 1 to 8 (“[a-e1-3].”, “d#”) – true(“[a-e1-3]”, “2”) – true (“[a-e1-3]”, “f2”) – false
xx yy Matches regex xx or yy

Java正则表达式元字符

在Java正则表达式中,我们有一些元字符,它们类似于常见匹配模式的短代码。

Regular Expression Description
\d Any digits, short of [0-9]
\D Any non-digit, short for [^0-9]
\s Any whitespace character, short for [\t\n\x0B\f\r]
\S Any non-whitespace character, short for [^\s]
\w Any word character, short for [a-zA-Z_0-9]
\W Any non-word character, short for [^\w]
\b A word boundary
\B A non word boundary

在正则表达式中,有两种方法可以将元字符用作普通字符。

    1. 在元字符之前加上反斜杠(\)。

 

    将元字符保持在 \Q(引用开始)和 \E(引用结束)之间。

Java中的正则表达式- 量词

Java的正则表达式量词用于指定要匹配的字符出现的次数。

Regular Expression Description
x? x occurs once or not at all
X* X occurs zero or more times
X+ X occurs one or more times
X{n} X occurs exactly n times
X{n,} X occurs n or more times
X{n,m} X occurs at least n times but not more than m times

Java正则表达式量词可以与字符类和捕获组一起使用。例如,[abc]+表示a、b或c出现一次或多次。(abc)+表示组“abc”出现一次或多次。我们现在将讨论捕获组。

Java中的正则表达式 – 捕获组。

在Java中,正则表达式的捕获组用于将多个字符视为单个单位。您可以使用()创建一个组。与捕获组匹配的输入字符串部分将保存到内存中,并可以使用反向引用进行调用。您可以使用matcher.groupCount方法来找出Java正则表达式模式中捕获组的数量。例如,((a)(bc))包含3个捕获组-((a)(bc)),(a)和(bc)。您可以在正则表达式中使用反向引用,使用反斜杠(\)和要调用的组的编号。捕获组和反向引用可能令人困惑,让我们通过一个例子来理解。

System.out.println(Pattern.matches("(\\w\\d)\\1", "a2a2")); //true
System.out.println(Pattern.matches("(\\w\\d)\\1", "a2b2")); //false
System.out.println(Pattern.matches("(AB)(B\\d)\\2\\1", "ABB2B2AB")); //true
System.out.println(Pattern.matches("(AB)(B\\d)\\2\\1", "ABB2B3AB")); //false

在第一个例子中,运行时第一个捕获组是(\w\d),当与输入字符串”a2a2″匹配并保存在内存中时,它的值为”a2″。因此,\1引用”a2″,并且返回true。由于同样的原因,第二个语句打印出false。请自行理解语句3和4的情况。现在我们来看一些Pattern类和Matcher类的重要方法。

    1. 我们可以使用标志位创建 Pattern 对象。例如,Pattern.CASE_INSENSITIVE 可以实现大小写不敏感的匹配。

 

    1. Pattern 类还提供了一个与 String 类 split() 方法类似的 split(String) 方法。

 

    1. Pattern 类的 toString() 方法返回编译此正则表达式的字符串。

 

    1. Matcher 类具有 start() 和 end() 索引方法,可以准确地显示匹配项在输入字符串中的位置。

 

    Matcher 类还提供了字符串处理方法 replaceAll(String replacement) 和 replaceFirst(String replacement)。

让我们在一个简单的示例程序中看看这些Java正则表达式方法。

package com.Olivia.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexExamples {

	public static void main(String[] args) {
		// using pattern with flags
		Pattern pattern = Pattern.compile("ab", Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher("ABcabdAb");
		// using Matcher find(), group(), start() and end() methods
		while (matcher.find()) {
			System.out.println("Found the text \"" + matcher.group()
					+ "\" starting at " + matcher.start()
					+ " index and ending at index " + matcher.end());
		}

		// using Pattern split() method
		pattern = Pattern.compile("\\W");
		String[] words = pattern.split("one@two#three:four$five");
		for (String s : words) {
			System.out.println("Split using Pattern.split(): " + s);
		}

		// using Matcher.replaceFirst() and replaceAll() methods
		pattern = Pattern.compile("1*2");
		matcher = pattern.matcher("11234512678");
		System.out.println("Using replaceAll: " + matcher.replaceAll("_"));
		System.out.println("Using replaceFirst: " + matcher.replaceFirst("_"));
	}

}

以上Java正则表达式示例程序的输出是什么。

Found the text "AB" starting at 0 index and ending at index 2
Found the text "ab" starting at 3 index and ending at index 5
Found the text "Ab" starting at 6 index and ending at index 8
Split using Pattern.split(): one
Split using Pattern.split(): two
Split using Pattern.split(): three
Split using Pattern.split(): four
Split using Pattern.split(): five
Using replaceAll: _345_678
Using replaceFirst: _34512678

关于Java中的正则表达式就介绍到这里。Java的正则表达式一开始看起来可能很难,但如果您在实际中使用一段时间,就会发现学习和使用它们变得很容易。

您可以从我们的GitHub存储库中查找完整的代码和更多的正则表达式示例。

发表回复 0

Your email address will not be published. Required fields are marked *