In Java, we typically implement these functions in conjunction with If-else, for, in order to find out whether a given string has a character or substring to look for, or to split a string, or to replace/delete some characters of a string. As shown below:
Java code
Public class test{
Public Static void Main (String args[]) {
String str="@Shang Hai Hong Qiao Fei Ji Chang";
Boolean rs = false;
for (int i=0; i<str.length (); i++) {
Char Z=str.charat (i);
if (' a ' = = Z | | ' F ' = = z) {
rs = true;
break;
}Else{
Rs= false;
}
}
System.out.println (RS);
}
}
Public class test{ public static void main (String args[]) { string str= "@Shang hai hong qiao fei ji chang "; boolean rs = false; for (Int i=0;i<str.length (); i++) { char z= Str.charat (i); if (' A ' == z | | ' F ' == z) { rs = true; break; }else{ rs= false; } } system.out.println (RS); }}
This method is simple and intuitive, but it is difficult to solve complex work, and the amount of code will increase a lot, not conducive to maintenance.
At this point, we can use regular expressions to implement these functions, and the code is simple and easy to maintain. Here are some common features of the regular expressions for strings in Java, as shown below (which uses the Java.util.regex package):
query a character or a substring in a string in 1.Java
Java code
String s = "@Shang Hai Hong Qiao Fei Ji Chang";
String regEx = "a| F "; //denotes a or F
Pattern Pat = Pattern.compile (regEx);
Matcher mat = Pat.matcher (s);
Boolean rs = Mat.find ();
String s = "@Shang Hai Hong Qiao Fei Ji Chang"; String regEx = "a| F "; Represents a or Fpattern Pat = Pattern.compile (regEx); Matcher mat = Pat.matcher (s); Boolean rs = Mat.find ();
If there is a regex in S, then RS is true, otherwise flase.
If you want to ignore the case when searching, you can write the Pattern pat=pattern.compile (regex,pattern.case_insensitive).
2. Get a string in a file
Java code
-
string regex = ". +\ (. +) $" ;
-
String s = "C:\test.txt" ;
-
Pattern pat = pattern.compile (regEx);
-
Matcher mat = pat.matcher (s);
-
boolean rs = mat.find ();
-
for ( int i= 1 ; I<=mat.groupcount (); i++) {
-
System.out.println (Mat.group (i));
-
}
String regEx = ". +\ (. +) $"; String s = "C:\test.txt"; Pattern Pat = Pattern.compile (regEx); Matcher mat = Pat.matcher (s); Boolean rs = Mat.find (); for (int i=1;i<=mat.groupcount (); i++) {System.out.println ( Mat.group (i));}
The above results are test.txt, the extracted string is stored in Mat.group (i), where I maximum value is mat.groupcount ();
3. Splitting a string
Java code
String regex=":";
Pattern Pat = Pattern.compile (regEx);
String[] rs = pat.split ("AA:BB:CC");
String regex= ":"; Pattern Pat = Pattern.compile (regEx); String[] rs = pat.split ("aa:bb:cc");
After execution, R is {"AA", "BB", "CC"}
If you use regular expression segmentation as shown above, generally we will use the following simpler method:
Java code
String s = "aa:bb:cc";
String[] Rs=s.split (":");
String s = "AA:BB:CC"; String[] Rs=s.split (":");
4. Substitution/deletion of strings
Java code
String regex="@+"; //denotes one or more @
Pattern Pat=pattern.compile (regEx);
Matcher Mat=pat.matcher ("@ @aa @b [email protected]@");
String S=mat.replaceall ("#");
String regex= "@+"; Represents one or more @pattern pat=pattern.compile (regEx); Matcher mat=pat.matcher ("@@[email protected] [email protected]@"); String S=mat.replaceall ("#");
The result is "# #aa #b cc##"
If you want to delete the @ in the string, just replace it with an empty string:
Java code
String S=mat.replaceall ("");
String S=mat.replaceall ("");
The result is "AaB cc"
Note: Description of the pattern class: The 1.public final class Java.util.regex.Pattern is the expression after the regular expression is compiled. The following statement creates a Pattern object and assigns a value to the handle pat:pattern Pat = Pattern.compile (regEx); Interestingly, the pattern class is the final class, and its constructor is private. Maybe someone told you something about the design pattern, or you could check the information yourself. The conclusion here is that the pattern class cannot be inherited, and we cannot create objects of the pattern class with new. Therefore, in the pattern class, 2 overloaded static methods are provided, whose return value is a reference to the Pattern object. Such as: Java code
Public Static Pattern compile (String regex) {
return New Pattern (Regex, 0);
}
public static pattern compile (String regex) {return new pattern (regex, 0);}
Of course, we can declare a handle to the pattern class, such as pattern pat = NULL; 2.pat.matcher (str) represents a match that uses pattern to generate a string str, and its return value is a reference to a Matcher class. We can simply use the following method: Boolean rs = Pattern.compile (regEx). Matcher (str). find (); |
attached : Common Regular Expressions: Match a specific number: ^[1-9]d*$//Match positive integer ^-[1-9]d*$//Match negative integer ^-? [1-9]d*$//Match integer ^[1-9]d*|0$//matches non-negative integers (positive integers + 0) ^-[1-9]d*|0$//matches a non-positive integer (negative integer + 0) ^[1-9]d*.d*|0.d*[1-9]d*$//Match positive floating point number ^-([1-9]d*.d*|0.d*[1-9]d*) $//Match negative floating point number ^-? ([1-9]d*.d*|0.d*[1-9]d*|0?. 0+|0) $//Match floating point number ^[1-9]d*.d*|0.d*[1-9]d*|0?. 0+|0$//Match non-negative floating-point number (positive floating point + 0) ^ (-([1-9]d*.d*|0.d*[1-9]d*)) | 0+|0$//matching non-positive floating-point number (negative floating-point number + 0) Commentary: useful when dealing with large amounts of data, with particular attention to remediation when applied
Match a specific string: ^[a-za-z]+$//Match a string consisting of 26 English letters ^[a-z]+$//matches a string consisting of 26 uppercase letters ^[a-z]+$//matches a string consisting of 26 letters in lowercase ^[a-za-z0-9]+$//matches a string consisting of a number and 26 English letters ^w+$//matches a string consisting of a number, 26 letters, or underscores
The validation features and their validation expressions when using RegularExpressionValidator validation controls are described below:
Only numbers can be entered: "^[0-9]*$" Only n digits can be entered: "^d{n}$" You can only enter at least n digits: "^d{n,}$" Only m-n digits can be entered: "^d{m,n}$" Only numbers starting with 0 and non-0 can be entered: "^ (0|[ 1-9][0-9]*) $ " You can only enter a positive real number with two decimal places: "^[0-9]+ (. [ 0-9]{2})? $ " You can only enter a positive real number with 1-3 decimal places: "^[0-9]+ (. [ 0-9]{1,3})? $ " You can only enter a non-zero positive integer: "^+?" [1-9] [0-9]*$] Only non-zero negative integers can be entered: "^-[1-9][0-9]*$" Only characters with a length of 3 can be entered: "^. {3}$ " You can only enter a string consisting of 26 English letters: "^[a-za-z]+$" You can only enter a string consisting of 26 uppercase English letters: "^[a-z]+$" You can only enter a string consisting of 26 lowercase English letters: "^[a-z]+$" You can only enter a string consisting of a number and 26 English letters: "^[a-za-z0-9]+$" You can only enter a string consisting of a number, 26 letters, or underscores: "^w+$" Verify user password: "^[a-za-z]w{5,17}$" is in the correct format: start with a letter, the length is between 6-18,
Only characters, numbers, and underscores can be included. Verify that it contains ^%& ',; =?$ ' characters: ' [^%& ',; =? $x 22]+ " Only Chinese characters can be entered: "^[u4e00-u9fa5],{0,}$" Verify email Address: "^w+[-+." w+) *@w+ ([-.] w+) *.w+ ([-.] w+) *$ " Verify InternetURL: "^http://([w-]+.) +[w-]+ (/[w-./?%&=]*)? $ " Verify phone Number: "^ ((d{3,4}) |d{3,4}-)? d{7,8}$"
The correct format is: "Xxxx-xxxxxxx", "xxxx-xxxxxxxx", "xxx-xxxxxxx",
"Xxx-xxxxxxxx", "XXXXXXX", "XXXXXXXX". Verify the Social Security number (15-bit or 18-digit number): "^d{15}|d{}18$" Validation 12 months of the year: "^ (0?[ 1-9]|1[0-2]) $ "correct format:" 01 "-" 09 "and" 1 "" 12 " Verify one months of 31 days: "^ ((0?[ 1-9]) | ((1|2) [0-9]) |30|31) $ "
The correct format is: "01" "09" and "1" "31".
Regular expressions that match Chinese characters: [U4E00-U9FA5] Match double-byte characters (including kanji): [^x00-xff] Regular expression that matches a blank line: n[s|] *r Regular expression matching HTML tags:/< (. *) >.*|< (. *)/>/ Regular expression matching the leading and trailing spaces: (^s*) | (s*$) Regular expression matching email address: w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) * Regular expression matching URL URL:/http ([w-]+.) +[w-]+ (/[w-./?%&=]*)? |
Use of regular expressions in Java