In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "case analysis of the use of Java regular expressions". In daily operation, I believe that many people have doubts about the use of Java regular expressions. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Java regular expressions use case analysis". Next, please follow the editor to study!
I. Overview:
Used to describe or match a series of strings that conform to the rules of a statement
II. Single symbol
1. English full stop. Symbol: matches a single arbitrary character.
The expression t.o can match: tno,t#o,teo and so on. Can not match: tnno,to,Tno,t positive o, etc.
2. Square brackets []: only the characters specified in square brackets participate in the matching, and only a single character can be matched.
Expression: t [abcd] n can only match: tan,tbn,tcn,tdn. Cannot match: thn,tabn,tn, etc.
3. | symbol. Equivalent to "or", you can match the specified characters, but only one of them can be selected to match.
Expression: t (a | b | c | dd) n can only match: tan,tbn,tcn,tddn. Cannot match taan,tn,tabcn and so on.
4. A symbol that represents the number of matches
Expression: [0mur9] {3}\-[0mur9] {2}\-[0mur9] {3} the matching format is: 999Mur99mur999
Because the-symbol has a special meaning in the regular expression, it represents a range, so the escape character\ is preceded by an escape character.
5. ^ symbol: indicates no, if used in square brackets, ^ indicates a character that you do not want to match.
Expression: [^ x] the first character cannot be x
6.\ s symbol: non-empty character
7,\ s symbol: empty character, can only match one space, tab, carriage return, page feed, can not match multiple spaces entered by yourself.
8.\ r symbol: space character, same as\ n,\ tab
III. Shortcut symbols
1.\ d means [0Mui 9]
2,\ D means [^ 0-9]
3.\ w means [0-9A-Z_a-z]
4,\ W means [^ 0-9A-Z_a-z]
5.\ s means [\ t\ n\ r\ f]
6.\ s means [^\ t\ n\ r\ f]
4. Regular expressions commonly used
1. Java: (([amurz] | _) (\\ w *)) {6 match 20} string that begins with a letter or underscore and ends with an alphanumeric underscore
2. JavaScript:/ ^ (\ -?) (\ d +) $/ matching number
3. JavaScript:/ ^\ alphanumeric underscore / match alphanumeric underscore.
Application of regular expressions in Java 1. Judgment function
Public boolean matches (String regex)
Case: determine whether the entered mobile phone number starts with 13 or 18
Package Lemon;import java.util.Scanner;public class RegexDm {public static void main (String [] args) {Scanner sc = new Scanner (System.in); System.out.println ("Please enter a good phone:"); String s = sc.nextLine (); String regex = "1 [38]\\ d {9}"; / / define the good rules for mobile phones boolean flag = s.matches (regex) / / judgment function System.out.println ("flag:" + flag);}} 2, segmentation function
Public String [] split (String regex)
Case study:
Package Lemon;import java.util.Scanner;public class RegexDm {public static void main (String [] args) {String age = "18-24"; / / define the age range String regex = "-"; String [] strArr = age.split (regex); / / split into string arrays int startAge = Integer.parseInt (strArr [0]); int endAge = Integer.parseInt (strArr [1]) Scanner sc = new Scanner (System.in); System.out.println ("Please enter your age:"); int a = sc.nextInt (); if (a > = startAge & & a)?
The IP address in the extracted information: (\ d +)\. (\ d +)
The Chinese mobile phone number extracted from the information: (86) * 0mm 13\ d {9}
The Chinese fixed phone number extracted from the information: (\ (\ d {3pr 4}\) |\ d {3pr 4}-|\ s)?\ d {8}
The Chinese phone number (including mobile and landline phones) in the extracted information: (\ (\ d {3pr 4}\) |\ d {3pr 4}-|\ s)?\ d {7pr 14}
The postcode of China in the extracted information: [1-9] {1} (\ d+) {5}
Extract the floating point number (that is, decimal) in the information: (-?\ d *)\.?\ d +
Extract any number from the information: (-?\ d *) (\.\ d +)?
IP address: (\ d +)\. (\ d +)
Phone area code: / ^ 0\ d {2jin3} $/
Tencent QQ: ^ [1-9] * [1-9] [0-9] * $
Account number (beginning with letters, 5-16 bytes allowed, alphanumeric underscores allowed): ^ [a-zA-Z] [a-zA-Z0-9 _] {4jue 15} $
Chinese, English, numbers and underscores: ^ [\ u4e00 -\ u9fa5_a-zA-Z0-9] + $
VII. Examples
1. Validate QQ number (requirements: 5-15 digits, 0 cannot start)
Package Lemon;import java.util.Scanner;public class RegexDm {public static void main (String [] args) {Scanner sc = new Scanner (System.in); System.out.println ("Please enter your QQ number:"); String qq = sc.nextLine (); System.out.println ("checkQQ:" + checkQQ (qq)) } private static boolean checkQQ (String qq) {return qq.matches ("[1-9] {1}\\ d {4pm 14}");}} at this point, the study on "Java regular expression using case analysis" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.