Java 模式類別是 java.util 套件中提供的公共最終類,用於建立正規表示式的編譯表示。所有正規表示式都轉換為此類的實例。它們用於建立 Matcher 類別的實例,該類別由 matches 方法組成,該方法比較以取得與正規表示式相符的字串,如果字串符合則傳回 true,否則傳回 false。該類別的實例是不可變的並且是線程安全的,這意味著它的實例對於並發工作的不同線程的工作沒有影響。
下面給的是java模式類別方法:
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
此方法有助於根據給定的正規表示式建立模式。
Parameters | The regular expression to be compiled in the form of string.
Example: ( .*) ( \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 ()); } }
代碼:
輸出:
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 類別的實例。
myregex:要編譯的字串形式的正規表示式。
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。
範例:代碼:
輸出:
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 標誌 ()
此方法有助於取得在編譯正規表示式以建立模式類別實例時設定的標誌的整數值。
範例: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個參數,不會拋出異常。
它是一個字元序列,我們需要以模式物件的形式將已編譯的正規表示式實例與它相符。
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. |
Exception:
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.
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:
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:
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:
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:
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中文網其他相關文章!