[JAVA] Regular expressions

Regular expressions

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]

Regular expression notation

[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.

When writing in JAVA.

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
      }

   }
}

Regular expressions

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

Regular expression example sentences

[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.

Regarding escape statements.

Describe with a yen mark (backslash). I think \ n is basically good.

Output on the console.

① My name is Yamada. System.out.println ("My name is Yamada.");

② My name is "Yamada". System.out.println ("My name is " Yamada \ ".");

END


  1. a-z ↩︎

  2. A-Z ↩︎

  3. a-zA-Z ↩︎

Recommended Posts

Regular expressions
Notes on regular expressions
colorize and regular expressions
[Java] Summary of regular expressions
About regular expressions in Ruby
Learn regular expressions little by little ①
Rails: Capture regular expressions in emails!
Parse Japanese addresses with regular expressions
Implementation of validation using regular expressions
Name a group of regular expressions (Java)
Easily make troublesome regular expressions with Rubular
Regular expressions that match 99% of email addresses
Easy to trip with Java regular expressions
Switch beans with @ConditionalOnExpression and SpEL regular expressions
About Ruby regular expressions (metacharacters, captures, related methods)
Match IP addresses using regular expressions in Java
Regular expression basics
JS regular expression
Ruby regular expression
About regular expressions used in ruby sub method
Distinguish between integers and decimals with regular expressions
[Ruby] Exclude and replace specific patterns with regular expressions
[RSpec] WebMock handles regular expressions and Array query strings
Notes on how to use regular expressions in Java