Current location - Trademark Inquiry Complete Network - Overdue credit card - Regular expressions about java
Regular expressions about java

The question is not clear about what technology is used to implement it, and what are the comparison requirements. You can post a tutorial first and you can read it first

Regular expressions

"^\\d+$" //Non-negative integer (positive integer + 0)

"^[0-9]*[1-9][0-9]*$" //Positive integer

"^((-\\d+)|())$" //Non-positive integer (negative integer + 0)

"^-[0-9] *[1-9][0-9]*$" //Negative integer

"^-?\\d+$" //Integer

"^\\d+( \\.\\d+)?$" //Non-negative floating point number (positive floating point number + 0)

"^(([0-9]+\\.[0-9]*[ 1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1 -9][0-9]*))$" //Positive floating point number

"^((-\\d+(\\.\\d+)?)|((\\ .)?))$" //Non-positive floating point number (negative floating point number + 0)

"^(-(([0-9]+\\.[0-9]* [1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[ 1-9][0-9]*)))$" //Negative floating point number

"^(-?\\d+)(\\.\\d+)?$" //Floating Points

"^[A-Za-z]+$" //A string consisting of 26 English letters

"^[A-Z]+$" //A string consisting of 26 A string consisting of 26 uppercase English letters

"^[a-z]+$" //A string consisting of 26 lowercase English letters

"^[A-Za -z0-9]+$" //A string consisting of numbers and 26 English letters

"^\\w+$" //A string consisting of numbers, 26 English letters or underscores

"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+ $" //email address

"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\ w+)*))*(\\?\\S*)?$" //url

"^[A-Za-z0-9_]*$"

Regular Detailed explanation of the use of expressions

Introduction

Simply put, regular expressions are a powerful tool that can be used for pattern matching and replacement. Its function is as follows:

Test a pattern of a string. For example, you can test an input string to see if a phone number pattern or a credit card number pattern exists in the string. This is called data validity verification.

Replacement text. You can use a regular expression to identify specific text in a document, which can then be deleted entirely or replaced with other text.

Extracts a substring from a string based on a pattern match. Can be used to find specific words in a text or input field.

Basic syntax

After having a preliminary understanding of the functions and effects of regular expressions, let's take a detailed look at the syntax format of regular expressions.

The form of the regular expression is generally as follows:

/love/ The part between the "/" delimiters is the pattern to be matched in the target object. Users only need to put the content of the pattern that they want to find matching objects between the "/" delimiters. In order to enable users to customize pattern content more flexibly, regular expressions provide special "metacharacters". The so-called metacharacters refer to those special characters with special meaning in regular expressions, which can be used to specify the appearance pattern of their leading characters (that is, the characters in front of the metacharacters) in the target object.

The more commonly used metacharacters include: "+", "*", and "?".

The "+" metacharacter stipulates that its leading character must appear one or more times continuously in the target object.

The "*" metacharacter stipulates that its leading character must appear zero times or multiple times in a row in the target object.

The "?" metacharacter specifies that its leading object must appear zero or once in the target object.

Now, let us take a look at the specific applications of regular expression metacharacters.

/fo+/ Because the above regular expression contains the "+" metacharacter, it means that it can appear consecutively with "fool", "fo", or "football" in the target object after the letter f. or strings matching multiple letters o.

/eg*/ Because the above regular expression contains the "*" metacharacter, it means that it can appear consecutively after the letter e with "easy", "ego", or "egg" in the target object. Matches a string of zero or more letters g.

/Wil?/ Because the above regular expression contains the "?" metacharacter, it means that it can be compared with "Win" or "Wilson" in the target object, etc. Zero or consecutive occurrences after the letter i Matches a string with the letter l.

Sometimes you don’t know how many characters to match. To accommodate this uncertainty, regular expressions support the concept of qualifiers. These qualifiers specify how many times a given component of the regular expression must occur to satisfy a match.

{n} n is a non-negative integer. Match a certain number of n times. For example, 'o{2}' does not match the 'o' in "Bob", but it does match both o's in "food".

{n,} n is a non-negative integer. Match at least n times. For example, 'o{2,}' does not match the 'o' in "Bob", but it matches all o's in "foooood". 'o{1,}' is equivalent to 'o+'. 'o{0,}' is equivalent to 'o*'.

{n,m} m and n are both non-negative integers, where n <= m. Match at least n times and at most m times. For example, "o{1,3}" will match the first three o's in "fooooood". 'o{0,1}' is equivalent to 'o?'. Please note that there cannot be a space between the comma and the two numbers.

In addition to metacharacters, users can specify exactly how often a pattern appears in matched objects. For example, /jim {2,6}/ The above regular expression stipulates that the character m can appear 2-6 times in a row in the matching object. Therefore, the above regular expression can match strings such as jimmy or jimmmmmy.

After having a preliminary understanding of how to use regular expressions, let's take a look at how to use several other important metacharacters.

\s: used to match a single space character, including tab keys and newline characters;

\S: used to match all characters except a single space character;

\d: used to match numbers from 0 to 9;

\w: used to match letters, numbers or underscore characters;

\W: used to match All characters that do not match \w;

.: used to match all characters except newline characters.

(Explanation: We can regard \s and \S and \w and \W as inverse operations of each other)

Next, let’s take a look at how to use regular expressions through examples Use the above metacharacters in the formula.

/\s+/ The above regular expression can be used to match one or more space characters in the target object.

/\d000/ If we have a complex financial statement, we can easily find all the amounts totaling thousands of dollars through the above regular expression.

In addition to the metacharacters we introduced above, regular expressions also have another unique special character, namely the locator. Locators are used to specify where the matching pattern appears in the target object.

The more commonly used locators include: "^", "$", "\b" and "\B".

The "^" locator specifies that the matching pattern must appear at the beginning of the target string

The "$" locator specifies that the matching pattern must appear at the end of the target object

< p>The "\b" locator specifies that the matching pattern must appear at one of the two boundaries at the beginning or end of the target string

The "\B" locator specifies that the matching object must be located at the end of the target string Within the two boundaries of the beginning and the end, that is, the matching object cannot be used as the beginning or the end of the target string. Similarly, we can also regard "^" and "$" and "\b" and "\B" as two sets of locators that are inverse operations of each other. For example: /^hell/ Because the above regular expression contains the "^" locator, it can match the string starting with "hell", "hello" or "hellhound" in the target object. /ar$/ Because the above regular expression contains the "$" locator, it can match strings ending with "car", "bar" or "ar" in the target object. /\bbom/ Because the above regular expression pattern starts with the "\b" locator, it can match strings starting with "bomb", or "bom" in the target object. /man\b/ Because the above regular expression pattern ends with the "\b" locator, it can match strings in the target object that end with "human", "woman" or "man".

In order to facilitate users to set matching patterns more flexibly, regular expressions allow users to specify a range in the matching pattern without being limited to specific characters. For example:

/[A-Z]/ The above regular expression will match any uppercase letter in the range from A to Z.

/[a-z]/ The above regular expression will match any lowercase letter in the range from a to z.

/[0-9]/ The above regular expression will match any number in the range from 0 to 9.

/([a-z][A-Z][0-9])+/ The above regular expression will match any string consisting of letters and numbers, such as "aB0". One thing that users need to pay attention to here is that you can use "()" in regular expressions to combine strings together. The content contained in the "()" symbol must also appear in the target object. Therefore, the above regular expression will not match a string such as "abc" because the last character in "abc" is a letter and not a number.

If we want to implement a regular expression similar to the "OR" operation in programming logic and select any one of multiple different patterns for matching, we can use the pipe character "|". For example: /to|too|2/ The above regular expression will match "to", "too", or "2" in the target object.

There is also a more commonly used operator in regular expressions, namely the negation operator "[^]". Different from the locator "^" we introduced earlier, the negation character "[^]" specifies that the string specified in the pattern cannot exist in the target object. For example: /[^A-C]/ The above string will match any character except A, B, and C in the target object. Generally speaking, when "^" appears inside "[]", it is regarded as a negative operator; when "^" is located outside "[]", or there is no "[]", it should be regarded as a negative operator. locator.

Finally, when users need to add metacharacters to the regular expression pattern and find their matching objects, they can use the escape character "\". For example: /Th\*/ The above regular expression will match "Th*" instead of "The" etc. in the target object.

After a regular expression is constructed, it can be evaluated like a mathematical expression, that is, from left to right and in a priority order.

The priorities are as follows:

1. \ escape character

2. (), (?=), [] round brackets and square brackets

3. *, +, ?, {n}, {n,}, {n,m} qualifiers

4. ^, $, \anymetacharacter position and order

5. |“OR” operation