首页  >  文章  >  Java  >  Java程序示例:演示正则表达式中的转义字符

Java程序示例:演示正则表达式中的转义字符

王林
王林转载
2023-09-19 09:01:03672浏览

Java程序示例:演示正则表达式中的转义字符

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

正则表达式

它是一个正则表达式的缩写。它是一个API,允许用户定义有用于查找、修改和编辑字符串的字符串模式。正则表达式经常用于定义限制的字符串领域,例如电子邮件验证和密码。java.util.regex包包含了正则表达式。

转义字符

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

其中一些例子包括:

n - 在这个例子中,它用于在文本中添加一个新行。

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

转义字符的方法

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

例如,如果在正则表达式中未转义点(.),它将匹配任何一个字符,并产生不清晰的结果。

在Java Regex中,字符可以通过两种不同的方式进行转义,我们将在下面详细研究。

  • 使用Q和E转义

  • 使用反斜杠()转义

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

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

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

字母 Q 和 E 之间,所有字符均转义。

通常用于转义大量字符。

示例 1

以下程序演示了在正则表达式中使用点号的转义字符的工作原理。

<span style="font-size: 13.125px;">// 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
        </span>Pattern p1 = Pattern.compile("<span>\</span>\Q.<span>\</span>\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());
    }
}
</span>

输出

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对象进行匹配。

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

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

反斜杠可用于转义字符。

由于反斜杠字符是它自己的一个字符,因此我们需要两个反斜杠。

然后,字符将被转义。

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

Example 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("<span>\</span>\.");
        //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匹配输入字符串s1和s2,如果Pattern对象p1和p2与输入字符串匹配,则显示布尔值true,并且如果它们不匹配,则显示布尔值false。

本文阐述了在正则表达式中转义字符的方法。文章从讨论正则表达式和转义字符两个术语开始,介绍了两种方法及其实现,以便对这个主题有一个清晰的理解。

以上是Java程序示例:演示正则表达式中的转义字符的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除