Java パターン クラス

WBOY
リリース: 2024-08-30 16:00:50
オリジナル
174 人が閲覧しました

Java パターン クラスは、正規表現のコンパイルされた表現を作成するために使用される java.util パッケージで提供されるパブリック最終クラスです。すべての正規表現は、このクラスのインスタンスに変換されます。これらは、Matcher クラスのインスタンスを作成するために使用されます。このクラスは、正規表現に一致する文字列を取得するために比較し、文字列が一致する場合は true を返し、一致しない場合は false を返すmatches メソッドで構成されます。このクラスのインスタンスは不変であり、スレッドセーフです。つまり、そのインスタンスは、同時に動作する別のスレッドの動作に影響を与えません。

Java パターン クラスのメソッド

Java パターン クラスのメソッドを以下に示します:

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

1. public static パターンのコンパイル (String 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. public static パターンのコンパイル (String myregex, int flag) このメソッドは、正規表現と指定されたフラグを使用してパターン クラスのインスタンスを作成するのに役立ちます。

      パラメータ
    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. public int flags ()

    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. public 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 中国語 Web サイトの他の関連記事を参照してください。

  • 関連ラベル:
    ソース:php
    このウェブサイトの声明
    この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
    最新の問題
    人気のチュートリアル
    詳細>
    最新のダウンロード
    詳細>
    ウェブエフェクト
    公式サイト
    サイト素材
    フロントエンドテンプレート
    私たちについて 免責事項 Sitemap
    PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!