首頁 > Java > java教程 > 主體

Java程式範例:示範正規表示式中的轉義字符

王林
發布: 2023-09-19 09:01:03
轉載
696 人瀏覽過

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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!