The Java Regex vai Regulārā izteiksme ir API definējiet modeli virkņu meklēšanai vai manipulācijām ar tām .
To plaši izmanto, lai noteiktu ierobežojumus virknēm, piemēram, parolei un e-pasta apstiprināšanai. Pēc Java regex apmācības apguves jūs varēsiet pārbaudīt savas regulārās izteiksmes, izmantojot Java Regex Tester Tool.
Java Regex API nodrošina 1 saskarni un 3 klases java.util.regex iepakojums.
java.util.regex pakotni
Klases Matcher un Pattern nodrošina Java regulārās izteiksmes iespēju. Java.util.regex pakotne nodrošina šādas regulāro izteiksmju klases un saskarnes.
- MatchResult saskarne
- Saderības klase
- Rakstu klase
- PatternSyntaxException klase
Saderības klase
Tas īsteno MatchResult saskarne. Tas ir regex dzinējs ko izmanto, lai veiktu atbilstības darbības rakstzīmju secībā.
| Nē. | Metode | Apraksts |
|---|---|---|
| 1 | Būla atbilstības() | pārbaudiet, vai regulārā izteiksme atbilst modelim. |
| 2 | Būla atradums() | atrod nākamo izteiksmi, kas atbilst modelim. |
| 3 | Būla atrašana (int sākums) | atrod nākamo izteiksmi, kas atbilst paraugam no dotā sākuma numura. |
| 4 | Virknes grupa () | atgriež atbilstošo apakšsecību. |
| 5 | int start () | atgriež atbilstošās apakšsecības sākuma indeksu. |
| 6 | int end () | atgriež atbilstošās apakšsecības beigu indeksu. |
| 7 | int groupCount() | atgriež atbilstošās apakšsecības kopējo skaitu. |
Rakstu klase
Tas ir regulārās izteiksmes kompilētā versija . To izmanto, lai definētu regulārās izteiksmes programmas modeli.
| Nē. | Metode | Apraksts |
|---|---|---|
| 1 | statiskā modeļa kompilēšana (virknes regulārā izteiksme) | apkopo doto regulāro izteiksmi un atgriež modeļa gadījumu. |
| 2 | Atbilstības meklētājs (CharSequence ievade) | izveido saskaņotāju, kas atbilst norādītajai ievadei ar modeli. |
| 3 | statiskās Būla atbilstības (virknes regex, CharSequence ievade) | Tas darbojas kā kompilēšanas un atbilstības metožu kombinācija. Tas apkopo regulāro izteiksmi un saskaņo doto ievadi ar modeli. |
| 4 | String[] sadalījums (CharSequence ievade) | sadala doto ievades virkni ap noteiktā modeļa atbilstībām. |
| 5 | Virknes raksts () | atgriež regex modeli. |
Java regulāro izteiksmju piemērs
Ir trīs veidi, kā Java rakstīt regex piemēru.
import java.util.regex.*; public class RegexExample1{ public static void main(String args[]){ //1st way Pattern p = Pattern.compile('.s');//. represents single character Matcher m = p.matcher('as'); boolean b = m.matches(); //2nd way boolean b2=Pattern.compile('.s').matcher('as').matches(); //3rd way boolean b3 = Pattern.matches('.s', 'as'); System.out.println(b+' '+b2+' '+b3); }} Izmēģiniet to tagad Izvade
true true true
Regulāra izteiksme . Piemērs
. (punkts) apzīmē vienu rakstzīmi.
import java.util.regex.*; class RegexExample2{ public static void main(String args[]){ System.out.println(Pattern.matches('.s', 'as'));//true (2nd char is s) System.out.println(Pattern.matches('.s', 'mk'));//false (2nd char is not s) System.out.println(Pattern.matches('.s', 'mst'));//false (has more than 2 char) System.out.println(Pattern.matches('.s', 'amms'));//false (has more than 2 char) System.out.println(Pattern.matches('..s', 'mas'));//true (3rd char is s) }} Izmēģiniet to tagad Regex rakstzīmju klases
| Nē. | Rakstzīmju klase | Apraksts |
|---|---|---|
| 1 | [abc] | a, b vai c (vienkāršā klase) |
| 2 | [^abc] | Jebkura rakstzīme, izņemot a, b vai c (noliegums) |
| 3 | [a-zA-Z] | no a līdz z vai no A līdz Z, ieskaitot (diapazons) |
| 4 | [a-d[m-p]] | no a līdz d vai no m līdz p: [a-dm-p] (savienība) |
| 5 | [a-z&&[def]] | d, e vai f (krustojums) |
| 6 | [a-z&&[^bc]] | no a līdz z, izņemot b un c: [ad-z] (atņemšana) |
| 7 | [a-z&&[^m-p]] | no a līdz z, nevis no m līdz p: [a-lq-z] (atņemšana) |
Regulārās izteiksmes rakstzīmju klases Piemērs
import java.util.regex.*; class RegexExample3{ public static void main(String args[]){ System.out.println(Pattern.matches('[amn]', 'abcd'));//false (not a or m or n) System.out.println(Pattern.matches('[amn]', 'a'));//true (among a or m or n) System.out.println(Pattern.matches('[amn]', 'ammmna'));//false (m and a comes more than once) }} Izmēģiniet to tagad Regex kvantori
Kvantori norāda rakstzīmes gadījumu skaitu.
| Regex | Apraksts |
|---|---|
| X? | X notiek vienreiz vai vispār nenotiek |
| X+ | X notiek vienu vai vairākas reizes |
| X* | X notiek nulle vai vairāk reižu |
| X{n} | X notiek tikai n reizes |
| X{n,} | X notiek n vai vairāk reizes |
| X{y,z} | X notiek vismaz y reizes, bet mazāk nekā z reizes |
Regulāro izteiksmju rakstzīmju klases un kvantori Piemērs
import java.util.regex.*; class RegexExample4{ public static void main(String args[]){ System.out.println('? quantifier ....'); System.out.println(Pattern.matches('[amn]?', 'a'));//true (a or m or n comes one time) System.out.println(Pattern.matches('[amn]?', 'aaa'));//false (a comes more than one time) System.out.println(Pattern.matches('[amn]?', 'aammmnn'));//false (a m and n comes more than one time) System.out.println(Pattern.matches('[amn]?', 'aazzta'));//false (a comes more than one time) System.out.println(Pattern.matches('[amn]?', 'am'));//false (a or m or n must come one time) System.out.println('+ quantifier ....'); System.out.println(Pattern.matches('[amn]+', 'a'));//true (a or m or n once or more times) System.out.println(Pattern.matches('[amn]+', 'aaa'));//true (a comes more than one time) System.out.println(Pattern.matches('[amn]+', 'aammmnn'));//true (a or m or n comes more than once) System.out.println(Pattern.matches('[amn]+', 'aazzta'));//false (z and t are not matching pattern) System.out.println('* quantifier ....'); System.out.println(Pattern.matches('[amn]*', 'ammmna'));//true (a or m or n may come zero or more times) }} Izmēģiniet to tagad Regex metarakstzīmes
Regulārās izteiksmes metarakstzīmes darbojas kā īskodi.
| Regex | Apraksts |
|---|---|
| . | Jebkura rakstzīme (var atbilst vai neatbilst terminatoram) |
| d | Jebkuri cipari, mazāki no [0-9] |
| D | Jebkurš bez cipara, saīsinājums no [^0-9] |
| s | Jebkura atstarpes rakstzīme, saīsinājums no [ x0Bf ] |
| S | Jebkura rakstzīme, kas nav atstarpes, īss [^s] |
| In | Jebkura vārda rakstzīme, saīsinājums no [a-zA-Z_0-9] |
| IN | Jebkura rakstzīme, kas nav vārds, īss [^w] |
| Vārdu robeža | |
| B | Nevārdu robeža |
Regulārās izteiksmes metarakstzīmju piemērs
import java.util.regex.*; class RegexExample5{ public static void main(String args[]){ System.out.println('metacharacters d....');\d means digit System.out.println(Pattern.matches('\d', 'abc'));//false (non-digit) System.out.println(Pattern.matches('\d', '1'));//true (digit and comes once) System.out.println(Pattern.matches('\d', '4443'));//false (digit but comes more than once) System.out.println(Pattern.matches('\d', '323abc'));//false (digit and char) System.out.println('metacharacters D....');\D means non-digit System.out.println(Pattern.matches('\D', 'abc'));//false (non-digit but comes more than once) System.out.println(Pattern.matches('\D', '1'));//false (digit) System.out.println(Pattern.matches('\D', '4443'));//false (digit) System.out.println(Pattern.matches('\D', '323abc'));//false (digit and char) System.out.println(Pattern.matches('\D', 'm'));//true (non-digit and comes once) System.out.println('metacharacters D with quantifier....'); System.out.println(Pattern.matches('\D*', 'mak'));//true (non-digit and may come 0 or more times) }} Izmēģiniet to tagad Regulārās izteiksmes 1. jautājums
/*Create a regular expression that accepts alphanumeric characters only. Its length must be six characters long only.*/ import java.util.regex.*; class RegexExample6{ public static void main(String args[]){ System.out.println(Pattern.matches('[a-zA-Z0-9]{6}', 'arun32'));//true System.out.println(Pattern.matches('[a-zA-Z0-9]{6}', 'kkvarun32'));//false (more than 6 char) System.out.println(Pattern.matches('[a-zA-Z0-9]{6}', 'JA2Uk2'));//true System.out.println(Pattern.matches('[a-zA-Z0-9]{6}', 'arun$2'));//false ($ is not matched) }} Izmēģiniet to tagad
Regulārās izteiksmes 2. jautājums
/*Create a regular expression that accepts 10 digit numeric characters starting with 7, 8 or 9 only.*/ import java.util.regex.*; class RegexExample7{ public static void main(String args[]){ System.out.println('by character classes and quantifiers ...'); System.out.println(Pattern.matches('[789]{1}[0-9]{9}', '9953038949'));//true System.out.println(Pattern.matches('[789][0-9]{9}', '9953038949'));//true System.out.println(Pattern.matches('[789][0-9]{9}', '99530389490'));//false (11 characters) System.out.println(Pattern.matches('[789][0-9]{9}', '6953038949'));//false (starts from 6) System.out.println(Pattern.matches('[789][0-9]{9}', '8853038949'));//true System.out.println('by metacharacters ...'); System.out.println(Pattern.matches('[789]{1}\d{9}', '8853038949'));//true System.out.println(Pattern.matches('[789]{1}\d{9}', '3853038949'));//false (starts from 3) }} Izmēģiniet to tagad Java Regex Finder piemērs
import java.util.regex.Pattern; import java.util.Scanner; import java.util.regex.Matcher; public class RegexExample8{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); while (true) { System.out.println('Enter regex pattern:'); Pattern pattern = Pattern.compile(sc.nextLine()); System.out.println('Enter text:'); Matcher matcher = pattern.matcher(sc.nextLine()); boolean found = false; while (matcher.find()) { System.out.println('I found the text '+matcher.group()+' starting at index '+ matcher.start()+' and ending at index '+matcher.end()); found = true; } if(!found){ System.out.println('No match found.'); } } } } Izvade:
Enter regex pattern: java Enter text: this is java, do you know java I found the text java starting at index 8 and ending at index 12 I found the text java starting at index 26 and ending at index 30