It is used to express a character string pattern in one format. Inconsistencies in the data given in the forums can be annoying and are used to eliminate those parts.
In short, error checking
Regular expressions.txt
Postal code[678-0041] [Search]
[678]-[0041] [Search]
[0-9]{3} - [0-9]{4}
Enter the numbers 0-9 three times
Add a hyphen
Enter the numbers 0-9 four times
Error if not included
The notational feeling of the rules.
text.java
class Test{
public static void main(String[] args){
String strZip = request.getParameter("zip");
String zipPattern = "[0-9]{3}-[0-9]{4}";
//This is important Apply the above rules.
Pattern p = Pattern.compile(zipPattern);
if (p.matcher.(strZip).find()) { //find()so[true/false]Is passed as the return value.
//Zip code format
} else {
//Input format is error
}
}
}
By using javascript, it is also possible to run a warning when the focus is out.
symbol | Explanation of symbols |
---|---|
・ | Any one character. Omit the newline character |
* | Matches 0 or more repetitions of the previous character |
^ | The beginning of the line ^ (hat character) |
$ | End of line |
[ ] | Match any single character in parentheses,-Range can be specified with |
{ } | Describe the number of repetitions of the numerical value in parentheses. |
+ | Repeat the previous expression 0 or 1 times |
[1]+$
[2]+$
[3]+$
Phone number: ^ [0-9] {2,4}-[0-9] {2,4}-[0-9] {3,4} $
Specify the range of {} separated by commas
^ [\ w] + $: \ w Half-width alphanumeric characters ^ \ d {3} \-\ d {4} $: \ d Post office number
The symbol is described by \ w \ d.
Describe with a yen mark (backslash). I think \ n is basically good.
① My name is Yamada. System.out.println ("My name is Yamada.");
② My name is "Yamada". System.out.println ("My name is " Yamada \ ".");
END
Recommended Posts