Today, the company asked me to create an algorithm for filtering strings. I thought it would be a good choice to use regular expressions and checked the regular expression information, I must have thought about some good things.
Filter non-0-9 strings in the code. You only need to modify the Regular Expression Conditions to filter out the required strings ~~~.
Copy content to clipboard
Code:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class check {
public static String StringFilter(String str) throws PatternSyntaxException {
String regEx = "[^0-9]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll("").trim();
}
Public static void main (string [] ARGs ){
String STR = "sdfdsfsdfgfdg00004234fdgdfg6456456fdgdfhh4234523 points to the point where the Sifang's imports have been reduced by the end of the decline ";
System. Out. println (stringfilter (STR ));
// Filter the string.
}
}
Add the specified condition "[]"
[A-Z] the condition is limited to one character in the lowercase A to Z range.
[A-Z] conditions are limited to one character in the upper case A to Z range
[A-Za-Z] the condition is limited to one character in the lowercase A to Z or uppercase A to Z range.
[0-9] the condition is limited to one character in the lowercase 0 to 9 Range
[0-9a-z] the condition is limited to one character in the lowercase 0 to 9 or A to Z range.
[0-9 [A-Z] the condition is limited to one character (intersection) in the lowercase 0 to 9 or A to Z range)
[] Add ^ and then add the restriction "[^]"
[^ A-Z] the condition is limited to one character in the range of non-lowercase A to Z
[^ A-Z] conditions are limited to one character in the range of not uppercase A to Z
[^ A-Za-Z] the condition is limited to one character in the range of non-lowercase A to Z or uppercase A to Z.
[^ 0-9] the condition is limited to one character in the range of 0 to 9 in non-lowercase letters.
[^ 0-9a-z] the condition is limited to one character in the range of 0 to 9 or A to Z in non-lowercase letters.
[^ 0-9 [A-Z] the condition is limited to one character (intersection) in the range of non-lowercase 0 to 9 or A to Z)