replaceAll // Replace all pairs
replaceFirst // First replace
/**
Exclude non-alphabets.
[^ a-zA-Z]-> Other than the alphabet */ private static void testReplace() { String str = "fjslru7974294jlsjfJLJL+;]:@:UOIUO"; String regex = "[^a-zA-Z]"; System.out.println(str.replaceAll(regex, "")); } //結果fjslrujlsjfJLJLUOIUO
split // split
/**
Divide the numbers by and make an array.
[0-9] +-> Many numbers
\ d +-> Many numbers */ private static void testSplit() { String str = "jfslj47924fjslk24729djldja492824"; String regex = "[0-9]+"; System.out.println(Arrays.toString(str.split(regex))); } // Result [jfslj, fjslk, djldja]
matches // matching
Decimal judgment
/**
\ d +-> Many numbers
(\. \ d +)?-> 0 or 1 in parentheses
*/ private static void testMatchDouble() { String str = "10.5"; String regex = "\d+(\.\d+)?"; if(str.matches(regex)) { System.out.println(Double.parseDouble(str)); }else { System.out.println ("not double"); } }
Date determination
/**
\ d {4}-> 4 numbers
\ d {2}-> 2 numbers
Judgment of phone number
/**
\ d {10,12}-> 10-12 numbers
\ d {3,5}-> 3-5 numbers
-->-
*/ private static void testMatchTellphoneNumber() { String str = "080-1111-1111"; String regex1 = "\d{10,12}"; String regex2 = "\d{3,5}-\d{3,5}-\d{3,5}"; if(str.matches(regex1) || str.matches(regex2)) { System.out.println(str); }else { System.out.println ("not phone number"); } }
Email judgment
/**
\ w +-> Many alphabets
[a-zA-Z]-> Many alphabets
[a-zA-Z0-9 \ _ \-\.]-> Alphabet or __. */ private static void testMatchEmail() { String str = "[email protected]_c-d.com"; String regex = "[a-zA-Z][a-zA-Z0-9\\-\.]{5,29}@[a-zA-Z0-9\\-\.]+\.\w+";
if(str.matches(regex)) {
System.out.println(str);
}else {
System.out.println ("not E-mail"); } }
private static void textPattern() {
//文字列 String string = "da646dad4da64da54d6a4d5a4d6a4d5ae78w"; String regex = "[0-9]+"; // Divide by pattern Pattern pattern = Pattern.compile(regex); System.out.println(Arrays.toString(pattern.split(string))); // Divide by string System.out.println(Arrays.toString(string.split(regex))); } // Result [da, dad, da, da, d, a, d, a, d, a, d, ae, w] [da, dad, da, da, d, a, d, a, d, a, d, ae, w]
private static void testMatcher() {
String str = "2019-09-10";
String regex = "\\d{4}\\-\\d{2}\\-\\d{2}";
// Matching by Matcher Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); System.out.println(""+ matcher.matches()); // Matching by String System.out.println(""+str.matches(regex)); } // Result true true
regex | meaning | Remarks |
---|---|---|
abc | abc | |
\\n | new line | |
\\t | tab | |
[abc] | a or b or c | |
[^abc] | a,b,Other than c | |
[a-zA-Z] | Alphabet | |
[a-d[m-p]] | a-d or m-p | |
\\d | Numbers | |
\\D | Other than numbers | |
\\w | a-z A-With Z_ | [a-zA-Z_]Same as |
\\W | Other than those above |
regex | meaning | Remarks |
---|---|---|
\\d+ | One or more numbers | |
\\d? | Number 0 1 | |
\\d* | Number 0, 1 or more | |
\\d{5} | 5 numbers | |
\\d{5,10} | Numbers 5 or more and 10 or less | |
\\d{5,} | 5 or more numbers |
regex | meaning | Remarks |
---|---|---|
XY | Judgment X followed by Y | |
X|Y | Judgment X or Y | |
(X) | Group Judgment X |
Recommended Posts