A collection of commonly used Java regular expressions

  • 2020-04-01 01:31:05
  • OfStack

Only Numbers can be entered: "^[0-9]*$".
Only n bits: "^\d{n}$".
Can only enter at least n digits: "^\d{n,}$".
Can only enter m - n digits:. ^ \ d {m, n} $"
Only zero and non-zero starting Numbers can be entered: "^(0|[1-9][0-9]*)$".
Can only enter two decimal positive real Numbers: "^[0-9]+(.[0-9]{2})? $".
Only positive real Numbers with 1~3 decimal places can be entered: "^[0-9]+(.[0-9]{1,3})? $".
Can only enter positive non-zero integers: "^\+? [1-9] [0-9] * $".
Only non-zero negative integers can be entered: "^\-[1-9][]0-9"*$.
Only characters of length 3 can be entered: "^.{3}$".
You can only enter A string of 26 English letters: "^[a-za-z]+$".
You can only enter A string of 26 uppercase English letters: "^[a-z]+$".
You can only enter a string of 26 lowercase English letters: "^[a-z]+$".
You can only enter A string of Numbers and 26 English letters: "^[a-za-z0-9]+$".
You can only enter a string of Numbers, 26 letters, or underscores: "^\w+$".
Verify user password: "^[a-za-z]\w{5,17}$"
Verify that ^%& is present; ',; =? $\" characters: "[^%&',;=?$\x22]+".
Can only input Chinese characters: "^[\u4e00-\u9fa5]{0,}$"
Verify Email address: "^ \ w + (\ w + / - +.]) * @ \ w + ([-] \ w +) * \. \ w + ([-] \ w +) * $".
Validation InternetURL: "^ http://%28 [-] / \ w + \.) + [-] \ w + (/ [\ w - /? % & amp; =] *)? $".
Verify phone number: "^(\(\d{3,4}-)|\d{3.4}-)? \d{7,8}$" correct format is: "xxx-xxxxxxx", "xxx-xxxxxxxx", "xxx-xxxxxxx", "xxx-xxxxxxxx", "XXXXXXX" and "XXXXXXXX".
Verify id number (15 or 18 digits) : "^\d{15}|\d{18}$".
Verify 12 months of the year: "^(0? [1-9]|1[0-2])$" correctly formatted as: "01" ~ "09" and "1" ~ "12".
Verify 31 days of a month: "^((0? [1-9])|((1|2)[0-9])|30|31) 01" ~ "09" and 1" ~ "31".
Use regular expressions to limit text fields in web forms:

Onkeyup ="value=value.replace(/[^\u4E00-\u9FA5]/g, ")" onbeforepaste=" clipboarddata.setdata ('text', clipboarddata.getdata ('text'). Replace (/[^\u4E00-\u9FA5]/g, ")"

Onkeyup ="value=value.replace(/[^\uFF00-\uFFFF]/g, ")" onbeforepaste=" clipboarddata.setdata ('text', clipboarddata.getdata ('text'). Replace (/[^\uFF00-\uFFFF]/g, ")"

Onkeyup ="value=value.replace(/[^\d]/g, ")" onbeforepaste=" clipboarddata.setdata ('text', clipboarddata.getdata ('text'). Replace (/[^\d]/g,'))"

Onkeyup ="value=value.replace(/[\W]/g, ")" onbeforepaste=" clipboarddata.setdata ('text', clipboarddata.getdata ('text'). Replace (/[^\d]/g,'))"

A javascript program that extracts the file name from the URL address with a regular expression, and the result is page1

S = "/ / www.jb51.net/about.htm"
S = s.r eplace (/ (. * \ /) {0} ([. ^ \] +). * / ig, "$2")
Alert (s)

Match double byte characters (including Chinese characters) : [^\x00-\ XFF]

Application: calculate the length of a string (2 for a double-byte character, 1 for an ASCII character)

String. The prototype. Len = function () {return this. The replace ([^ \ x00 - \ XFF] / g, "aa"). The length; }

Regular expression matching empty line: \n[\s|]*\r

Regular expressions that match HTML tags: /< (. *) > . * < \ \ 1 / > | < (. *) \ / > /

Regular expression matching Spaces :(^\s*)|(\s*$)

String. The prototype. The trim = function ()
{
      Return this. The replace (/ (^ \ s *) | (\ s * $)/g, "");
}

Using regular expressions to decompose and convert IP addresses:

The following is a Javascript program that USES regular expressions to match the IP address and convert the IP address to the corresponding value:

The function IP2V (IP)
{
  Re = / (\ d +) \. (\ d +) \. (\ d +) \. (\ d +)/g  // a regular expression that matches the IP address
The (IP) if (re)
{
Return the RegExp. $1 * math.h pow (255, 3)) + RegExp. $2 * math.h pow (255, 2)) + RegExp. $3 * 255 + RegExp. $4 * 1
}
The else
{
  Throw new Error("Not a valid IP address!") )
}
}

However, if the above program does not use regular expressions, but directly use the split function to split, the program is as follows:

Var IP = "10.100.20.168"
IP = IP. The split (". ")
Alert (" IP value is: "+ (IP [0] * 255 * 255 * 255 + IP [1] * 255 * 255 * 255 + IP + IP [2] [3] * 1))
Symbol interpretation:

character
describe

\
Mark the next character as a special character, or as a literal character, or as a backward reference, or as an octal escape. For example, 'n' matches the character 'n'. '\n' matches a newline character. The sequence '\ 'matches '\' and '\ (' matches' ('.

^
Matches the starting position of the input string. If the Multiline property of the RegExp object is set, the ^ also matches the position after '\n' or '\r'.

$
Matches the end of the input string. If the Multiline property of the RegExp object is set, the $also matches the position before '\n' or '\r'.

*
Matches the preceding subexpression zero or more times. For example, zo* matches "z" and "zoo". * is equivalent to {0,}.

+
Matches the preceding subexpression once or more. For example, 'zo+' matches 'zo 'and' zoo ', but not 'z '. + is equivalent to {1,}.

?
Matches the preceding subexpression zero or once. For example, "do (es)?" Can match "do" in "do" or "does". ? Is equivalent to {0,1}.

{n}
N is a non-negative integer. It matches a certain number of n times. For example, 'o{2}' cannot match 'o' in 'Bob', but can match two o's in 'food'.

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

{n, m}
M and n are non-negative integers, where n < = m. At least n matches and at most m matches. For example, "o{1,3}" will match the first three o's in "fooooood". 'o{0,1}' is equivalent to 'o? '. Note that there is no space between the comma and the two Numbers.

?
When the character is immediately followed by any other qualifier (*, +,? , {n}, {n,}, {n,m}), the matching pattern is non-greedy. The non-greedy pattern matches as few strings as possible, while the default greedy pattern matches as many strings as possible. For example, for the string "oooo", 'o+? 'will match a single 'o', and 'o+' will match all 'o'.

.
Matches any single character except "\n". To match any character including '\n', use a pattern like '[.\n]'.

(pattern)
Match the pattern and get the match. The Matches obtained can be obtained from the generated Matches collection, using the SubMatches collection in VBScript, and $0 in JScript... $9 properties. To match the parenthesis character, use '\(' or '\)'.

(? : the pattern)
The matching pattern does not obtain the matching result, that is to say, it is a non-acquired match and will not be stored for later use. This is useful when using "or" characters (|) to combine parts of a pattern. For example, 'industr (? :y|ies) is a shorter expression than 'industry|industries'.

(? The pattern of =)
Forward preview, matching the find string at the beginning of any string matching pattern. This is a non-fetch match, that is, the match does not need to be fetched for later use. For example, 'Windows? =95|98|NT|2000)' matches "Windows" in "Windows 2000", but not "Windows" in "Windows 3.1". A preview does not consume characters, that is, after a match occurs, the search for the next match begins immediately after the last match, rather than after the character containing the preview.

(? ! The pattern)
Negative preview, matching the search string at the beginning of any string that does not match the pattern. This is a non-fetch match, that is, the match does not need to be fetched for later use. Such as' Windows (? ! 95|98|NT|2000)' matches "Windows" in "Windows 3.1", but not "Windows" in "Windows 2000". A preview does not consume characters, that is, after a match occurs, the search for the next match begins immediately after the last match, rather than after the character containing the preview

X | y
Match x or y. For example, 'z|food' can match 'z 'or' food'. '(z|f)ood' matches' zood 'or' food '.

[xyz]
Set of characters. Matches any of the contained characters. For example, '[ABC]' can match 'a' in 'plain'.

[^ xyz]
Set of negative characters. Matches any character not included. For example, '[^ ABC]' matches 'p' in 'plain'.

[a-z]
Character range. Matches any character in the specified range. For example, '[a-z]' can match any lowercase alphabetic character from 'a' to 'z'.

[^ a-z]
Negative character range. Matches any arbitrary character that is not in the specified range. For example, '[^a-z]' can match any arbitrary character that is not in the 'a' to 'z' range.

\ b
Match a word boundary, that is, the position between the word and the space. For example, 'er\b' can match 'er' in 'never', but not 'er' in 'verb'.

\ B
Matches nonword boundaries. Er \B' matches 'er' in 'verb', but not 'er' in 'never'.

\ cx
Matches the control character specified by x. For example, \cM matches a control-m or carriage return. The value of x must be either A minus Z or one of A minus Z. Otherwise, c is treated as a literal 'c' character.

\ d
Matches a numeric character. This is the same thing as 0 minus 9.

\ D
Matches a non-numeric character. This is the same thing as ^0-9.

\ f
Matches a page break. This is the same thing as \x0c and \cL.

\ n
Matches a newline character. This is the same thing as \x0a and \cJ.

\ r
Matches a carriage return. This is the same thing as \x0d and \cM.

\ s
Matches any whitespace characters, including Spaces, tabs, page breaks, and so on. Is equivalent to [\f\n\r\t\v].

\ S
Matches any non-whitespace characters. This is equivalent to [^ \f\n\r\t\v].

\ t
Matches a TAB character. This is the same thing as \x09 and \cI.

\ v
Matches a vertical TAB character. This is the same thing as \x0b and \cK.

\ w
Matches any word character that includes an underscore. This is the same thing as '[a-za-z0-9_]'.

\ W
Matches any non-word characters. This is the same thing as '[A- za-z0 -9_]'.

\ xn
Matches n, where n is the hexadecimal escape value. The hexadecimal escape value must be two digits long. For example, '\x41' matches' A '. '\x041' is equivalent to '\x04' & "1". ASCII encoding can be used in regular expressions. .

\ num
Matches num, where num is a positive integer. A reference to the match obtained. For example, '(.)\1' matches two consecutive identical characters.

\ n
Identifies an octal escape value or a backward reference. N is a backward reference if there are at least n subexpressions obtained before \n. Otherwise, if n is an octal digit (0-7), n is an octal escape value.

\ nm
Identifies an octal escape value or a backward reference. Nm is a backward reference if there are at least nm subexpression before \nm. If there are at least n fetuses before \nm, n is a backward reference followed by the word m. If none of the previous conditions are met, if n and m are octal digits (0-7), \nm will match the octal escape value nm.

\ NML
If n is an octal digit (0-3) and m and l are octal digits (0-7), the octal escape value NML matches.

\ UN
Matches n, where n is a Unicode character represented by four hexadecimal digits. For example, \u00A9 matches the copyright symbol (?) .


Related articles: