Kelas Corak Java ialah kelas akhir awam yang disediakan dalam pakej java.util yang digunakan untuk mencipta perwakilan tersusun bagi ungkapan biasa. Semua ungkapan biasa ditukar kepada contoh kelas ini. Ia digunakan untuk mencipta contoh kelas Matcher, yang terdiri daripada kaedah padanan yang membandingkan untuk mendapatkan rentetan yang sepadan dengan ungkapan biasa dan mengembalikan benar jika rentetan sepadan sebaliknya mengembalikan palsu. Contoh kelas ini tidak boleh diubah dan selamat untuk benang, yang bermaksud tika itu tidak mempunyai kesan dengan kerja urutan yang berbeza berfungsi serentak.
Diberikan di bawah ialah kaedah kelas corak java:
Mulakan Kursus Pembangunan Perisian Percuma Anda
Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain
Kaedah ini membantu mencipta corak daripada ungkapan biasa yang diberikan.
Parameters | The regular expression to be compiled in the form of string.
Example: ( .*) ( \hh) ( .*) |
PatternSyntaxException dilemparkan apabila sintaks ungkapan tidak sah dihantar ke kaedah.
Contoh:
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 ()); } }
Kod:
Output:
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. kompilasi Corak statik awam (String myregex, bendera int) Kaedah ini membantu mencipta contoh kelas Corak menggunakan ungkapan biasa dan bendera yang ditentukan.
myregex: Ungkapan biasa yang akan disusun dalam bentuk rentetan.
bendera: Bendera ialah spesifikasi yang perlu disediakan semasa menyusun ungkapan biasa. Contohnya ialah 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 dilemparkan apabila sintaks ungkapan tidak sah dihantar ke kaedah.
IllegalArgumentException dilemparkan jika nilai bit berbeza dengan nilai yang sepadan dengan bendera yang dihantar.
Contoh:Kod:
Output:
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. bendera int awam ()
Kaedah ini membantu mendapatkan nilai integer bendera yang telah ditetapkan semasa menyusun ungkapan biasa untuk mencipta contoh kelas corak.
Contoh: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. |
Kod:
Output:
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. padanan Penjodoh awam (CharSequenceinputSequence)
Kaedah ini digunakan untuk mencipta contoh objek pemadan dengan membandingkan ungkapan biasa dengan urutan input yang diberikan. Kaedah ini memerlukan 1 parameter untuk dilalui, dan tiada pengecualian akan dilemparkan.
Ia ialah jujukan aksara yang mana kita perlu memadankan contoh regex yang disusun dalam bentuk objek corak.
Parameter
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. |
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.
Atas ialah kandungan terperinci Kelas Corak Java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!