Java 演示正则表达式中的转义字符

Java 演示正则表达式中的转义字符

在这里,我们将通过Java程序演示正则表达式中的转义字符。在深入讨论该主题之前,让我们先了解一下转义字符和正则表达式这个术语。

正则表达式

它是正则表达式的缩写。它是一个API,可以让用户定义对于查找、修改和编辑字符串有用的字符串模式。正则表达式通常用于定义限制条件的字符串的几个领域,包括电子邮件验证和密码。java.util.regex包涵盖了正则表达式。

转义字符

当一个字符前面有一个反斜杠(\)时,它包括数字、字母和标点符号。编译器对这些字符进行不同的处理,这样的字符被称为转义字符。

一些示例包括:

\n – 它用于在此处的文本中添加一个新行。

' – 它用于在此处的文本中添加一个单引号字符。

转义字符的方法

为了在正则表达式中匹配特殊字符(如点(.)、井号(#)等),必须对其进行转义。

例如,如果在正则表达式中不对点(.)进行转义,它会匹配任何单个字符,并产生不明确的结果。

在Java正则表达式中,有两种不同的转义字符方法,我们将在下面详细讨论。

  • 使用\Q和\E进行转义

  • 使用反斜杠(\)进行转义

方法1. 使用Q和E进行转义

为了转义字符,我们可以使用Q和E转义序列。

转义序列以字母Q开头,以字母E结尾。

在字母Q和E之间,所有字符都被转义。

通常用于转义多个字符。

示例1

以下代码演示了正则表达式中使用点进行转义的转义字符的工作原理。

// Java Program to demonstrate how to escape characters in Java
// Regex Using \Q and \E for escaping

import java.io.*;
import java.util.regex.*;
//creation of a class named Regexeg1
public class Regexeg1 {
    // Main method
    public static void main(String[] args) {
        // providing two strings as inputs
        String s1 = "Tutorials.point";
        String s2 = "Tutorialspoint";
        //creation of an object of Pattern class with dot escaped
        Pattern p1 = Pattern.compile("\\Q.\\E");
        //creation of an object of Pattern class without escaping the dot
        Pattern p2 = Pattern.compile(".");
        // Matchers for every combination of patterns and strings
        Matcher m1 = p1.matcher(s1);
        Matcher m2 = p1.matcher(s2);
        Matcher m3 = p2.matcher(s1);
        Matcher m4 = p2.matcher(s2);
        // find whether p1 and p2 match and display the Boolean value as a result
        System.out.println("p1 matches s1: " + m1.find());
        System.out.println("p1 matches s2: " + m2.find());
        System.out.println("p2 matches s1: " + m3.find());
        System.out.println("p2 matches s2: " + m4.find());
    }
}

输出

p1 matches s1: true
p1 matches s2: false
p2 matches s1: true
p2 matches s2: true

在上面的Java程序中,演示了在正则表达式中使用\Q和\E来转义一串字符的用法。

创建了两个输入字符串s1和s2,以及两个Pattern对象p1和p2,其中p1使用\Q和\E来转义点字符”.”,而p2不转义点字符。

创建了四个Matcher对象m1、m2、m3和m4,用于将输入字符串与Pattern对象进行匹配。

最后,程序显示布尔值true,如果Pattern对象p1和p2使用Matcher对象m1、m2、m3和m4与输入字符串s1和s2匹配,则显示布尔值false,如果不匹配。

方法2. 使用反斜杠(//)作为转义字符

反斜杠可以用来转义字符。

由于反斜杠字符是它自己的一个字符,所以需要两个反斜杠。

然后,字符被转义了。

通常使用它来转义字符串末尾的字符。

示例2

下面的程序演示了使用反斜杠(//)作为转义字符的正则表达式中转义字符的工作原理。

// Java Program to demonstrate how to escape characters in Java
// Regex using backslash (\) for escaping
import java.io.*;
import java.util.regex.*;
//creation of a class named Regexeg2
public class Regexeg2 {
    public static void main (String[] args) {
        // providing two strings as inputs
        String s1="Tutorials.point";
        String s2="Tutorialspoint";
        //creation of an object of Pattern class with dot escaped
        Pattern p1=Pattern.compile("\\.");
        //creation of an object of Pattern class without dot escaped
        Pattern p2=Pattern.compile(".");
        //Four matchers for each pattern string combination
        Matcher m1=p1.matcher(s1);
        Matcher m2=p1.matcher(s2);
        Matcher m3=p2.matcher(s1);
        Matcher m4=p2.matcher(s2);
        // find whether p1 and p2 match and display the boolean value as a result
        System.out.println("p1 matches s1: "+m1.find());
        System.out.println("p1 matches s2: "+m2.find());
        System.out.println("p2 matches s1: "+m3.find());
        System.out.println("p2 matches s2: "+m4.find());
    }
}

输出

p1 matches s1: true
p1 matches s2: false
p2 matches s1: true
p2 matches s2: true

在上面的Java代码中,演示了在正则表达式中使用转义字符来匹配字符串中的特殊字符,使用反斜杠来转义。

这里创建了两个输入字符串s1和s2,以及两个Pattern对象p1和p2,其中p1通过反斜杠转义了点字符“.”,而p2则没有转义点字符。

然后创建了四个Matcher对象m1、m2、m3和m4,用于将输入字符串与Pattern对象进行匹配。

最后,程序通过使用Matcher对象m1、m2、m3和m4将Pattern对象p1和p2与输入字符串s1和s2进行匹配,并显示布尔值true,如果它们匹配,则显示布尔值false,否则显示布尔值false。

本文介绍了正则表达式中转义字符的方法。文章始于对术语正则表达式和转义字符的讨论,讨论了两种方法以及它们的实现,以便更清楚地理解这个主题。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程