首頁 > Java > java教程 > 主體

Java模式類

WBOY
發布: 2024-08-30 16:00:50
原創
274 人瀏覽過

Java 模式類別是 java.util 套件中提供的公共最終類,用於建立正規表示式的編譯表示。所有正規表示式都轉換為此類的實例。它們用於建立 Matcher 類別的實例,該類別由 matches 方法組成,該方法比較以取得與正規表示式相符的字串,如果字串符合則傳回 true,否則傳回 false。該類別的實例是不可變的並且是線程安全的,這意味著它的實例對於並發工作的不同線程的工作沒有影響。

Java 模式類別方法

下面給的是java模式類別方法:

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

1.公共靜態模式編譯(字串myregex)

此方法有助於根據給定的正規表示式建立模式。

Parameters The regular expression to be compiled in the form of string.

Example: ( .*) ( \hh) ( .*)

參數

要編譯為字串形式的正規表示式。 範例:
    ( .*) ( \hh) ( .*)
表>
  • 例外:

    當傳遞給方法的表達式的語法無效時,會拋出PatternSyntaxException。

    範例:

    import java.util.regex.Pattern;
    public class HelloWorld {
    private static final String MYREGEX =  " ( .*) (Learn) ( .*)?";
    public static void main (String[] args) {
    Pattern pattern = Pattern.compile (MYREGEX);
    System.out.println ( "Regular Expression " +pattern.pattern ());
    }
    }
    登入後複製

    代碼:

    Java模式類

    輸出:

    Parameters

     

    a. myregex: The regular expression to be compiled in the form of string.
    b. flag: Flags are the specifications that need to be provided while compiling a regular expression. Example are CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE.

    2.公用靜態模式編譯(字串myregex,int標誌) 此方法有助於使用正規表示式和指定標誌建立 Pattern 類別的實例。

      參數
    •  
    a。

    myregex:要編譯的字串形式的正規表示式。

    b。

    flag: 標誌是編譯正規表示式時所需提供的規範。例如 CASE_INSENSITIVE、MULTILINE、DOTALL、UNICODE_CASE。

    表> 例外:
    import java.util.regex.Pattern;
    public class HelloWorld {
    private static final String REGEX =  " ( .*) (Learn) ( .*)?";
    public static void main (String[] args) {
    Pattern pattern = Pattern.compile (REGEX, Pattern.CASE_INSENSITIVE);
    System.out.println ( "Regular Expression " +pattern.pattern ());
    }
    }
    登入後複製

    當傳遞給方法的表達式的語法無效時,會拋出PatternSyntaxException。

    如果位元值與傳遞的標誌相符的值不同,則會拋出 IllegalArgumentException。 Java模式類

    範例:

    代碼:

    輸出:

    import java.util.regex.Pattern;
    public class HelloWorld {
    private static final String REGEX =  " ( .*) (Learn) ( .*)?";
    public static void main (String[] args) {
    Pattern pattern = Pattern.compile (REGEX, Pattern.UNICODE_CHARACTER_CLASS );
    System.out.println ( "Flag for UNICODE_CHARACTER_CLASS " +pattern.flags ());
    Pattern pattern2= Pattern.compile (REGEX, Pattern.UNICODE_CASE );
    System.out.println ( "Flag for UNICODE_CASE " +pattern2.flags ());
    }
    }
    登入後複製

    3.公 int 標誌 ()

    Java模式類此方法有助於取得在編譯正規表示式以建立模式類別實例時設定的標誌的整數值。

    範例:

    Parameters inputSequence: It is a sequence of character to which we need to match the compiled instance of regex in the form of a pattern object.

    代碼:

    輸出:

    import java.util.regex.MatchResult;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class HelloWorld {
    private static final String MYREG=  " ( .*) (gu)";
    private static final String SEQ ="Learning regular expression helps a lot in Artificial Intelligence";
    public static void main (String[] args) {
    Pattern pattern = Pattern.compile (MYREG, Pattern.UNICODE_CHARACTER_CLASS );
    Matcher matcherObj = pattern.matcher (SEQ);
    if (matcherObj.find ()) {
    MatchResult res = matcherObj.toMatchResult ();
    System.out.println ( "The offset  : "+res.end ());
    }
    }
    }
    登入後複製

    4.公用 Matcher 匹配器 (CharSequenceinputSequence)

    此方法用於透過將正規表示式與給定的輸入序列進行比較來建立匹配器物件的實例。此方法需要傳遞1個參數,不會拋出異常。 Java模式類

    參數

    inputSequence:

    它是一個字元序列,我們需要以模式物件的形式將已編譯的正規表示式實例與它相符。

    表>
    Parameters inputSequence: It is a sequence of character to which we need to match the compiled instance of regex in the form of a pattern object.
    MyRegex: The regular expression to be compiled in the form of string.
    範例: 代碼: 輸出: 5. public static booleanmatches (String MyRegex, CharSequenceinputSequence) 此方法用於將給定的輸入序列與正規表示式的編譯版本進行比對。 參數 inputSequence: 它是一個字元序列,我們需要將模式物件形式的正規表示式的已編譯實例與之相符。 MyRegex: 要編譯的字串形式的正規表示式。 表>

    Exception:

    • PatternSyntaxException is thrown when the syntax of the expression is invalid being passed to the method.

    6. public String pattern ()

    This method is used to get the regular expression from which the pattern instance has been compiled. This method requires no parameters to be passed as well as no exception will be thrown.

    An example for this method is shown below with an example of the quote method.

    7. public static String quote (String s)

    This method is used to get the literal string after the pattern is resolved using the method’s flags. This method requires 1 parameter to be passed as well as no exception will be thrown.

    Parameters The string of regular expression that needs to be compiled.

    Example:

    Code:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class HelloWorld {
    private static String myRegx = "location$";
    private static String line= "The location$ office have great capacity of seats for our employess";
    private static String newEx = "Bangalore";
    public static void main (String[] args) {
    Pattern patternObj2 = Pattern.compile (Pattern.quote (myRegx));
    Matcher matcherObj1 = patternObj2.matcher (line);
    line= matcherObj1.replaceAll (newEx);
    System.out.println ( "The Regular expression defined for pattern instance is " +patternObj2.pattern ());
    System.out.println ( "The Input sequence after substitution of Regular Expression  "+ line);
    }
    }
    登入後複製

    Output:

    Java模式類

    8. public String[] split (CharaterSequence in)

    This method is used to split the input String argument at every index where regular expression is found. This method requires 1 parameter to be passed as well as no exception will be thrown.

    Parameters  input: The character sequence that is to be split according to the Regular expression specified.

    Example:

    Code:

    import java.util.regex.Pattern;
    public class HelloWorld {
    private static String REGEX = ":";
    private static String INPUT = "BANGLORE:CHENNAI:GUWAHATI:AHMEDABAD";
    public static void main (String[] args) {
    Pattern patternObj2 = Pattern.compile (REGEX);
    String[] result = patternObj2.split (INPUT);
    for (String data:result){
    System.out.println (data+"\n");
    }
    }
    }
    登入後複製

    Output:

    Java模式類

    9. public String[] split (CharaterSequencein, intmyThreshHold)

    This method also splits the given input sequence but upto some threshold. This method requires 2 parameters to be passed as well as no exception will be thrown.

    Parameters a. in: The character sequence that is to be split.
    b. myThreshHold: This parameter defines the threshold of splitting operation.

    Example:

    Code:

    import java.util.regex.Pattern;
    public class HelloWorld {
    private static String REGEX = ":";
    private static String INPUT = "BANGLORE:CHENNAI:GUWAHATI:AHMEDABAD";
    public static void main (String[] args) {
    Pattern patternObj2 = Pattern.compile (REGEX);
    String[] result = patternObj2.split (INPUT,3);
    for (String data:result){
    System.out.println (data+"\n");
    }
    }
    }
    登入後複製

    Output:

    Java模式類

    10. public String toString ()

    This method is used to get the String representation of the regular expression from which the regular expression has been compiled. This method requires no parameters to be passed as well as no exception will be thrown.

    Example:

    Code:

    import java.util.regex.Pattern;
    public class HelloWorld {
    private static String REGEX = "location$";
    public static void main (String[] args) {
    Pattern patternObj2 =
    Pattern.compile (Pattern.quote (REGEX));
    System.out.println ( "toString representation of pattern instance is " +patternObj2.toString ());
    }
    }
    登入後複製

    Output:

    Java模式類

    Conclusion

    Pattern class is used to store the compiled representation of regular expressions from where they are passed to an instance of Matcher class to get the strings that match that regular expression. In this way, one is able to work with regular expressions more efficiently in their application.

    以上是Java模式類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

  • 相關標籤:
    來源:php
    本網站聲明
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
    熱門教學
    更多>
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板