Regular expressions , Also known as regular expression .( English :Regular Expression, In code it is often abbreviated as regex、regexp or RE), A concept of computer science . Regular expressions are often used for retrieval 、 Replace those that match a pattern ( The rules ) The text of . Many programming languages support string manipulation with regular expressions . for example , stay Perl A powerful regular expression engine is built in . The concept of regular expression was originally developed by Unix Tool software in ( for example sed and grep) Popular . Regular expressions are usually abbreviated to “regex”, singular Yes regexp、regex, The plural Yes regexps、regexes、regexen. Regular expressions are for strings ( Include normal characters ( for example ,a To z Between the letters ) And special characters ( be called “ Metacharacters ”)) A logical formula for operation , It is to use some specific characters defined in advance 、 And the combination of these specific characters , Form a “ Rule string ”, This “ Rule string ” A filter logic used to express strings . Regular expressions are a text pattern , This pattern describes one or more strings to match when searching for text .

Given a regular expression and another string , We can achieve the following goals :

1. Whether the given string conforms to the filtering logic of regular expression ( Referred to as “ matching ”):

2. You can use regular expressions , Get the specific part we want from the string .

Regular expressions are characterized by :

1. flexibility 、 Very logical and functional ;

2. Complex control of strings can be achieved quickly and in a very simple way .

3. For new contacts , It's more obscure .

Because the main application object of regular expression is text , So it has applications in all kinds of text editor occasions , Small to famous editor EditPlus, As big as Microsoft Word、Visual Studio And so on , You can use regular expressions to handle text content .

Regular expressions (regular expression) Describes a pattern of string matching (pattern), Can be used to check if a string contains a certain seed string 、 Replace a matched substring or remove a substring from a string that meets a condition, etc .

for example :

  • runoo+b, Can match runoob、runooob、runoooooob etc. ,+ The number indicates that the preceding character must appear at least once (1 Times or times ).
  • runoo*b, Can match runob、runoob、runoooooob etc. ,* The number indicates that the character does not appear , One or more times (0 Time 、 or 1 Time 、 Or many times ).
  • colou?r  Can match color perhaps colour,? The question mark represents that the preceding character can only appear once at most (0 Time 、 or 1 Time ).

The way to construct a regular expression is the same as the way to create a mathematical expression . That is, you can combine small expressions with many metacharacters and operators to create larger expressions . A regular expression component can be a single character 、 Character set 、 character in range 、 Choice between characters or any combination of all these components .

Regular expressions are made up of ordinary characters ( Such as character a To z) And special characters ( be called " Metacharacters ") The composition of the text pattern . Pattern describes one or more strings to match when searching for text . Regular expression as a template , Match a character pattern to the string being searched .

Why regular expressions ?

Typical search and replace operations require you to provide exact text that matches the expected search results . Although this technique may be sufficient for simple search and replace tasks on static text , But it lacks flexibility , If you use this method to search dynamic text , Even if it's not impossible , At least it will be very difficult .

By using regular expressions , Sure :

  • Test the pattern within the string .
    for example , You can test the input string , To see if phone number mode or credit card number mode appears in the string . This is called data validation .
  • replace text .
    Regular expressions can be used to identify specific text in a document , Delete the text completely or replace it with other text .
  • Extract substrings from strings based on pattern matching .
    You can find specific text in a document or in an input field .

for example , You may need to search the entire website , Delete obsolete material , And replace some HTML Format mark . under these circumstances , You can use regular expressions to determine whether the material or the HTML Format mark . This process reduces the list of affected files to those that contain materials that need to be removed or changed . You can then use regular expressions to remove obsolete materials . Last , You can use regular expressions to search for and replace tags .

Verify name , mailbox , cell-phone number , password

import java.util.regex.Pattern;
/**
* Account related attribute verification tool
*
*/
public class AccountValidatorUtil {
/**
* Regular expressions : Verify user name
*/
public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,20}$"; /**
* Regular expressions : Verify password
*/
public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,20}$"; /**
* Regular expressions : Verify phone number
*/
public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"; /**
* Regular expressions : Verify email
*/
public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; /**
* Verify username
*
* @param username
* @return Verify by returning true, Otherwise return to false
*/
public static boolean isUsername(String username) {
return Pattern.matches(REGEX_USERNAME, username);
} /**
* Check the password
*
* @param password
* @return Verify by returning true, Otherwise return to false
*/
public static boolean isPassword(String password) {
return Pattern.matches(REGEX_PASSWORD, password);
} /**
* Check cell phone number
*
* @param mobile
* @return Verify by returning true, Otherwise return to false
*/
public static boolean isMobile(String mobile) {
return Pattern.matches(REGEX_MOBILE, mobile);
} /**
* Checkbox
*
* @param email
* @return Verify by returning true, Otherwise return to false
*/
public static boolean isEmail(String email) {
return Pattern.matches(REGEX_EMAIL, email);
}
}

Java RE ( Regular expressions ) More articles about

  1. java Basic usage of regular expressions in

    Regular expressions are a specification that can be used for pattern matching and replacement , A regular expression is made up of ordinary characters ( Such as character a To z) And special characters ( Metacharacters ) The composition of the text pattern , it It is used to describe one or more strings to be matched when searching for text body . Regular expressions as ...

  2. java Basics ---->java Regular expression 2

    Classes related to regular expressions are :Pattern.Matcher and String. Today we start Java Regular expression learning in . Pattern and Matcher The understanding of the One . How to use regular expressions The recommended methods are as follows ...

  3. Java Regular expression of

    package RegexTest; /** * Created by hu on 2016/3/29. */ /* * Java Regular expression of In regular expressions , use \d Represents a number , If used in other languages ...

  4. Java And regular expressions

    Java And regular expressions label : Java Basics Regular As the regular name shows, it describes a rule , Use this rule to match strings . Learning regular expressions is learning the grammar rules of regular expressions Regular grammar Ordinary character Letter , Numbers , Chinese characters , ...

  5. Java Regular expression removal in html label

    Java Regular expression removal in html The label of , The main purpose is to display the content more accurately , For example, some time ago, I was doing something similar to blog post function , After inputting content in the editor, the style label will also be transferred to the background and saved in the database , But when you display the summary , For example, display positive ...

  6. Java object-oriented Regular expressions

     Java object-oriented Regular expressions A summary of knowledge :                (1) Features of regular expressions (2) Matching of regular expressions (3) Regular expression cuts , Replace , obtain (4) Regular expression exercises Regular expressions : accord with ...

  7. Java Common regular expressions ,Java Regular expressions ,Java Verification of ID card , The latest mobile number regular expression

    Java Common regular expressions ,Java Regular expressions ,Java Verification of ID card , The latest mobile phone number verification regular expression ============================== Copyright  Sweet potato 2017 year 11 ...

  8. JAVA There are four common methods of regular expression in

    JAVA Four common methods of dealing with strings by regular expressions in : matching . Division . Replace . Intercept . It is similar to the common functions of string , But it's easier to use regular expressions . More concise . Here are some concrete examples : public class TestRegex ...

  9. Java Support for regular expressions ( One )

    Java Support for regular expressions is mainly reflected in String.Pattern.Matcher and Scanner class . 1.Pattern.Matcher First look at one Pattern and Matcher Class using regular expressions . ...

  10. Java Use regular expressions to determine whether the date format is correct

    1.Java Use regular expressions to determine whether the date format is correct DateType.java: /** * @Title:DateType.java * @Package:com.you.dao * @Descript ...

Random recommendation

  1. Nginx Detailed explanation of Chinese 、 Configuration deployment and high concurrency optimization

      One .Nginx Common commands : 1. start-up Nginx          /usr/local/nginx/sbin/nginxpoechant@ubuntu:sudo ./sbin/nginx2. stop ...

  2. JS There are some ways to delay page execution ( Arrangement )

    Generally in JS There are some ways to delay page execution . You can use the following methods  jQuery.delay() Method introduction  http://shawphy.com/2010/11/jquery-delay.html jQuery in que ...

  3. android Study 17—— The command line builds gradle engineering

    Use the command line to build gradle The project involves several versions of the tools . 1.gradle Version of . This one uses gradle -version find out . 2.SDK BuildTools Version of . This needs to be prepared first SDK Environment variables of , And then in ...

  4. Html and Css Learning notes -css Basic knowledge of

    My email address :zytrenren@163.com Welcome to exchange learning error correction ! This blog post is my review note ,html and css It took too long to learn , Almost forgotten , I'm going to use it recently , So open it again html Skim the book , After recording the label ...

  5. keyword new And malloc function

    Make a question bug,OJ Say I don't have a definition new. what ?new You also need to define ? It's all direct use , Clearly in their own compiler can pass ! compiler : Labor and capital are C++.... After a toss, I found that , I am using C++, however OJ The compiler for is C, ...

  6. JavaEE Mind mapping

  7. DLNg-CNN The first week

    1. Edge detection example * Represents convolution operation , Standard said . Use 3*3 It's convoluted by the filter , take 3*3 On the left side , And add the results : Step 2: move the window one unit to the right , Calculate ... Move the pane down one more time after horizontal , Cycle through .. ...

  8. vb6 Use msxml2.serverxmlhttp request HTTP

    Private Sub Command1_Click() Command1.Enabled = False GetHtmlStr Text1 End Sub Private Sub GetHtmlSt ...

  9. USB Learning notes - summary

    1. # ls /sys/bus/usb/devices/ analysis :1-0:1.0 1-1 1-1:1.0 2-0:1.0 2-1 2-1:1.0 2-2 2-2.1 2-2:1.0 2-2.1:1.0 ...

  10. MySQL Data table operation (DDL)

    One . Create data table grammar :create table The name of the table ( Field   Field type [ Field properties ], Field   Field type [ Field properties ],...) [ Table options ]; Table options : Properties of the data table , Generally include engine.charset. ...