Packagecom. regular expressions;ImportJava.util.Scanner;/*** * Check QQ number * 1: required must be 5-15 digits * 2:0 cannot start * Analysis: * A: Keyboard input QQ number * B: Write a function * C: Call function , Output results **/ Public classRegexdemo { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); System.out.println ("Please enter your QQ number:"); String QQ=Sc.nextline (); System.out.println ("CHECKQQ:" +CHECKQQ (QQ)); } /*** Write an implementation check * Two explicit: * Return value type: Boolean * parameter list: String QQ*/ Public Static Booleancheckqq (String qq) {Booleanflag=true; if(Qq.length () >=5 && qq.length () <=15) {//Number of digits if(!qq.startswith ("0")) {//Start Char[] Cha=Qq.tochararray (); for(inti=0;i<cha.length;i++) {//content if(!character.isdigit (Cha[i])) {Flag=false; Break; } } }Else{flag=false; } }Else{flag=false; } returnFlag; }}the introduction of regular
Packagecom. regular expressions;ImportJava.util.Scanner;/*** Regular expression: A string that conforms to a certain rule **/ Public classRegexDemo2 { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); System.out.println ("Please enter your QQ number:"); String QQ=Sc.nextline (); System.out.println ("CHECKQQ:" +CHECKQQ (QQ)); } Public Static Booleancheckqq (String qq) {//String regex= "[1-9][0-9]{4,14}"; //Public Boolean matches (string regex) tells whether this string matches a regular expression//Boolean flag=qq.matches (QQ); returnQq.matches ("[1-9][0-9]{4,14}"); }}the introduction of regular 2
The regular expression requires:
A: Character x character X. Example: ' A ' represents the character a \ \ backslash character \ n line break (' \u000a ') \ r return character (' \u000d 'B: Character class [ABC] A, B or C (Simple Class) [^ABC] except for the one character outside of ABC [a-za-Z] A to Z or A to Z, the letters at both ends are included [0-9] 0-characters within 9 include the C: predefined character class. Represents any character. Represents itself using \ Escape \d number 0-9,[0-9] \d Non-numeric \s non-whitespace characters \s non-whitespace characters \w word characters: [a-za-z_0-9The things that make up a word inside a regular expression must consist of these things d: boundary character^Line at the beginning of the $ line at the end of the \b Word character is not a word character where example: Hello World?Haha;xixie: quantity word X?x appears 0 times or once x*x appears 0 or more times x+x appears 1 or more times X{n} X appears exactly n times x{n,} x appears at least n times x{n,m} x appears at least n times, but not more than M times f: Character set literal escape \x grouping [...] Range a-Z-Set [a-e][i-U] intersection [a-z&&[Aeiou]]
Packagecom. regular expressions. _2;ImportJava.util.Scanner;/** * Judgment function * Public Boolean matches string (string regex) * * Requirements: * Determine whether the mobile phone number meets the function * Analysis: * A: Keyboard entry mobile phone number Code * B: Define mobile phone number and rule * 1 start, 11 digits, all numbers * C: Call function, Judge can * D: output RESULTS * Requirements: * Check mailbox * Analysis: * A: Keyboard input mobile phone number * B: Define the rules of the mailbox * 1 start, 11 digits, all numbers * C: Call function, Judge can * D: output result*/ Public classRegexDemo01 { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); System.out.println ("Please enter your email address:"); String Email=Sc.nextline (); //String regex= "[A-za-z_0-9][email protected][a-za-z_0-9]{2,6} (\\.[ a-za-z_0-9]{2,3}) + ";String regex= "\\[email protected]\\w{2,6} (\\.\\w{2,3}) +"; Booleanflag=email.matches (regex); SYSTEM.OUT.PRINTLN (flag); } Public BooleanPdphone () {Scanner SC=NewScanner (system.in); System.out.println ("Please enter your mobile phone number:"); String Phone=Sc.nextline (); String Regex= "[1]\\d{10}"; Booleanflag=phone.matches (regex); returnFlag; }}Judging
Packagecom. regular expressions. _2;Importjava.util.Arrays;ImportJava.util.Scanner;/** Split function * public string[] Split (string regex) * Split string according to given regular expression * * Example: * Lily net, Century Jia Yuan, cherish the net, QQ * Search for Friends * Gender: Female * Range: "18-24" * * age>=18 && age<=24 * * Define a string, split * * The "91 27 46 38 50" output will eventually be "*/ Public classRegexDemo02 { Public Static voidMain (string[] args) {String str1= "91 27 46 38 50"; String[] Strarray=str1.split (""); int[] arr=New int[Strarray.length]; for(inti=0;i<strarray.length;i++) {Arr[i]=Integer.parseint (Strarray[i]); } arrays.sort (arr); StringBuilder SB=NewStringBuilder (""); for(inti=0;i<arr.length;i++) {sb.append (Arr[i]+" "); } System.out.println (SB); } Public voidtest02 () {String S1= "AA,BB,CC"; String[] Str1array=s1.split (","); for(inti=0;i<str1array.length;i++) {System.out.println (str1array[i]); } System.out.println ("------------------------"); S1= "aa.bb.cc"; Str1array=s1.split ("\ \")); for(inti=0;i<str1array.length;i++) {System.out.println (str1array[i]); } System.out.println ("-------------------"); S1= "AA bb cc"; //str1array=s1.split ("\\s+");Str1array=s1.split ("+"); for(inti=0;i<str1array.length;i++) {System.out.println (str1array[i]); } System.out.println ("-------------------"); S1= "E:\\javase\\day14\\avi"; Str1array=s1.split ("\\\\"); for(inti=0;i<str1array.length;i++) {System.out.println (str1array[i]); } System.out.println ("-------------------"); } Public voidtest01 () {//Define an age search rangeString ages= "18-24"; String Regex="-"; String[] Strarray=ages.split (regex); /*for (int x=0;x<strarray.length;x++) {System.out.println (strarray[x]); }*/ intStartage=integer.parseint (strarray[0]); intEndage=integer.parseint (strarray[1]); Scanner input=NewScanner (system.in); System.out.println ("Please enter your Age:"); intAge=Input.nextint (); if(Age>=startage && age<=endage) {System.out.println ("You are what I want to find."); }Else{System.out.println ("Not in line with my requirements."); } }}Split
Packagecom. regular expressions. _2;/** Regular expressions are used to replace: * public string ReplaceAll (string regex,string replacement) replaces all substrings in this string that match the given regular expression with the given string * PU Blic string Replacefirst (string regex,string replacement) replaces the first substring in this string that matches a given regular expression with the given string*/ Public classRegexDemo03 { Public Static voidMain (string[] args) {String s= "Helloqq12345worldkh12313423java"; String Regex= "\\d+"; //String regex= "\\d";String ss= "*";//String Result=s.replaceall (regex, SS);//System.out.println (result);String result=S.replacefirst (regex, SS); SYSTEM.OUT.PRINTLN (result); }}Replace
Packagecom. regular expressions. _2;Importjava.util.Arrays;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;/* * Before using the method in string to manipulate regular expressions to determine, cut, and replace strings * if you want to use the Find function, the string class cannot be reached. * * Pattern * mode Regular expression of the compiled expression form * It is mainly used to create the corresponding match object and then use the matching object method to match * version 1.4 start * No construction, * compile (String rege x) get the Match object * pattern () returns the regular expression in which this pattern was compiled * string[] Split (charsequence input) splits the input sequence based on the matching of this pattern * mat Ches (charsequence input) creates a match for this pattern * static matchers (charsequence input), equivalent to compile method and Matcher method one step * MATC Her * matches the engine that performs the matching operation on character sequence by parsing the pattern * using it generally performs three different matching operations: * Matches attempts to match the entire input sequence to the pattern * Lookingat try to match the input sequence from scratch to the pattern, unlike matches, which does not need to match the entire string * Find tries to find the next subsequence of the input sequence matching that pattern find with Lookingat The difference is the "next" * Method: * int End () returns the offset after the last matching character, that is, its subscript * Boolean find () * Boolean find (int start) resets the match, and then from the The index begins to find * String Group () returns the input subsequence matched by the previous match operation, which is the return of the matched input string sequence * int Group () returns the number of matched sequences * Boolean lookingat () tasting Try to match the input sequence starting at the beginning of the zone with the pattern * quotreplacement (string s) returns the literal substitution string for the specified string, in phaseWhen ReplaceAll * Matcher region (int start,int end) in string sets the match range * int Regionstart () returns the start index of the matching region of the match * int r Egionend () Returns the end index of the matching region of the match*/ Public classPatternandmatcher { Public Static voidMain (string[] args) {testfind (); } Public voidTestpattern () {String s= "FGHGF sashg SAS sa"; String Regex= "\\s+"; Pattern P=pattern.compile (regex); String[] Strarray=P.split (s); System.out.println (arrays.tostring (Strarray)); System.out.println (P.pattern ()); } Public Static voidTestfind () {String s= "Hello, nice to meet!" I ' m Guo Zhen, let's me play with game! "; String Regex= "\\b\\w{3}\\b"; Pattern P=pattern.compile (regex); Matcher m=P.matcher (s); while(M.find ()) {String S1=M.group (); System.out.println (S1); } }}find and constructors and patterns
Regular, string usage, Pattern Matcher