Padrões comuns de expressões regulares

Consulte padrões de expressões regulares para e-mail, URL, telefone, números e validação de texto. Use exemplos como referência de código.

AnnotationsRegular Expression
Website (URL)[a-zA-z]+://[^\s]*
IP Address (IP Address)((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)
Email Address\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
QQQ number.[1-9]\d{4,}
HTML tags (include content or self-contained)<(.*)(.*)>.*<\/\1>|<(.*) \/>
Password (composed of numeric/facile/low letter/point symbol, all four must be in place, more than eight places)(?=^.{8,}$)(?=.*\d)(?=.*\W+)(?=.*[A-Z])(?=.*[a-z])(?!.*\n).*$
Date (year-month-year)(\d{4}|\d{2})-((1[0-2])|(0?[1-9]))-(([12][0-9])|(3[01])|(0?[1-9]))
Date (month/day/year)((1[0-2])|(0?[1-9]))/(([12][0-9])|(3[01])|(0?[1-9]))/(\d{4}|\d{2})
Time (hours: minutes, 24 hours)((1|0?)[0-9]|2[0-3]):([0-5][0-9])
Han (char)[\u4e00-\u9fa5]
Chinese and full agglomerations (charts)[\u3000-\u301e\ufe10-\ufe19\ufe30-\ufe44\ufe50-\ufe6b\uff01-\uffee]
Fixed telephone number in mainland China(\d{4}-|\d{3}-)?(\d{8}|\d{7})
Chinese mobile phone number1\d{10}
Chinese Postal Code[1-9]\d{5}
Chinese mainland ID number (15 or 18)\d{15}(\d\d[0-9xX])?
Non-negative integer (positive or zero)\d+
Positive[0-9]*[1-9][0-9]*
Negative Integer-[0-9]*[1-9][0-9]*
Integer-?\d+
Decimal(-?\d+)(\.\d+)?
Do not include abc words\b((?!abc)\w)+\b
AnnotationsRegular Expression
Username/^[a-z0-9_-]{3,16}$/
Password/^[a-z0-9_-]{6,18}$/
Hex value/^#?([a-f0-9]{6}|[a-f0-9]{3})$/
E-mail/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
URL/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
IP Address/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
HTML Tag/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/
Hanword range in Unicode code/^[u4e00-u9fa5],{0,}$/
Regular expression matching Chinese characters[\u4e00-\u9fa5]
Comment: It's a headache to match Chinese. It's an expression.
Match two bytes (including Han)[^\x00-\xff]
Comment: can be used to calculate the length of a string (one two-byte character length count 2, ASCII characters count 1)
Regular expression matching blank lines\n\s*\r
Commentary: This could be used to remove blank lines
Regular expression matching HTML tags<(\S*?)[^>]*>.*?</\1>|<.*?/>
Comment: The online version is too bad, it's only a match, and there's nothing left to do about complex nesting tags.
Regular expression matching blank characters at the end^\s*|\s*$
Comment: A very useful expression that can be used to delete blank characters (including spaces, tabs, page breaks, etc.) at the end of the line
Regular expression matching Email address\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
Commentary: Useful form validation
Regular expression matching URL URL[a-zA-z]+://[^\s]*
Commentary: The limited functionality of the online version, which basically meets the demand above
Match the validity of account numbers (letter beginning, 5-16 bytes allowed, alphanumeric underlined)^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Commentary: Useful form validation
Match domestic phone numbers\d{3}-\d{8}|\d{4}-\d{7}
Commentary: Matching forms such as 0511-4405222 or 021-878882
Match to QQQ.[1-9][0-9]{4,}
Commentary: QQ Q Q from 10,000
Matching China Mainland Postal Code[1-9]\d{5}(?!\d)
Commentary: mainland China has a 6-digit postal code
Match ID\d{15}|\d{18}
Commentary: 15 or 18 identity cards in mainland China
Matchip Address\d+\.\d+\.\d+\.\d+
Commentary: Useful when extracting ip addresses
Matches a specific number:
^[1-9]\d*$// Match positive integer
^-[1-9]\d*$// Match negative integers
^-?[1-9]\d*$// Match the integer
^[1-9]\d*|0$// Matching non-negative integers (positive integer + 0)
^-[1-9]\d*|0$// Matching Integers (negative integers + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$// Match positive floating points
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$// Match negative floating points
^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$// Matching Floats
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$// Match non-negative floats (positive floats + 0)
^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$// Match non-positive floats (negative floats + 0)
Commentary: Useful in processing large amounts of data, with care to amend when applying them
Match specific strings
^[A-Za-z]+$// Match a string of 26 letters
^[A-Z]+$// Match a string of 26 letters in uppercase
^[a-z]+$// Match a string of 26 letters of lowercase
^[A-Za-z0-9]+$// Match a string of numbers and 26 letters
^\w+$// Match strings consisting of numbers, 26 letters or underlineds
CharacterDescription
\Marks the next character as a special character, or as an original character, or as a backward reference, or as an octal convert. For example, 'n' matches the character 'n'. '\n' matches a line break. Series "\\" matches "\\" and "\() matches "().
^Matches the beginning of the input string. If the Multiline properties of the RegExp object are set, ^ matches the position after " \\n " or " \\r ".
$Matches the end of the input string. If the Multiline properties of the RegExp object are set, the $ matches the position before " \\n " or " \\r ".
*Matches the preceding subexpression zero times or more. For example, zo* matches "z " and "zoo " * equals {0,}.
+Matches the previous sub-expressions once or more. For example, " zo+" matches "zo" and "zoo" but does not match "z". + equals {1,}.
?Matches the previous subexpression zero or one time. For example, "do(es)?" matches "do" in "do" or "does"? The equivalent is {0,}.
{n}n is a non-negative integer. Matches the established n times. For example, "o{2}" does not match "o" in "Bob" but matches two os in "food".
{n,}n is a non-negative integer. For example, “o{2,}” does not match “o” in “Bob”, but it matches all o in “foooood”. “o{1,}” equals “o+”. “o{0,}” equals “o*”.
{n,m}m and n are both non-negative integers, of which n< =m. The minimum match is n and maximum is m. For example, “o{1,3}” will match the first three o in “foooood”. “o{0,}” equals “o?”. Note that there is no room between comma and two numbers.
?When the character follows any other limiter (*, +,?, {n}, {n,}, {n,}, {m}), the matching pattern is not greedy. The non-vory mode matches the search for as few strings as possible, while the default greed pattern matches the search for as many strings as possible. For example, the string "oo", "o+?" will match a single "o" and the "o+" will match all "o".
.Matches any single character except " \n ". To match any character including " \n ", use a pattern like "[.\n] ".
(pattern)Matches the pattern and obtains this match. Matches can be obtained from the generated Matches collection, uses SubMatches collections in VBScript, and $0.$9 in JScript. To match round brackets, use '\() or '\'.
(?:pattern)Matching pattern without a matching result, which means that it is not a matching match and is not stored for later use. This is useful when using or character "(|)" to combine parts of a pattern. For example, "industr" is a simpler expression than "industry |industries".
(?=pattern)This is a non-required match, that is, the match does not need to be accessed for future use. For example, " Windows (??=95|NT|2000) " matches " Windows 2000 ", but does not match " Windows" in "Windows3.1 ". The preview does not consume characters, that is, the next matching search begins immediately after the last match, and does not start after the prechecking.
(?!pattern)Negative pre-check, matching the search string at the beginning of any string that does not match the pattern. This is a non-acquisition match, that is, the match does not need to be retrieved for later use. For example, " Windows(??95|NT|2000)" matches "Windows" in "Windows3.1", but does not match "Windows" in "Windows2000". The pre-check does not consume characters, that is, the next match search begins immediately after the last match has taken place, instead of starting with the character containing the preview.
x|ymatches x or y. For example, "z|food" matches "z" or "food". "(z|f)ood" matches "zood" or "food".
[xyz]character set. Match any character included. For example, '[abc]' can match 'a' in 'plain'.
[^xyz]Negative-value character collections. Match any character that is not included. For example, '[^abc]' matches 'p' in 'plain'.
[a-z]character range. Match any character within the specified range. For example, " [a-z] " can match any lowercase characters within the range " a " to " z ".
[^a-z]Negative value character range. Matches any character that is not within the specified range. For example, '[^a-z]' matches any character that is not within the range of 'a' to 'z'.
\bMatches a word boundary, that is, the position between a word and a space. For example, "er\b" matches "er" in "never", but does not match "er" in "verb".
\BMatches the non-word boundary. "er\B" matches "er" in "verb", but does not match "er" in "never".
\cxMatches the control character specified by x. For example, \cM matches a Control-M or a return sign. The value of x must be one of A-Z or a-z. Otherwise, c is considered to be a original "c" character.
\dMatches a numerical character. The value is equal to [0-9].
\DMatches a non-numeric character. The value is [^0-9].
\fMatches a page break. The value is equal to \\x0c and \\cl.
\nMatches a line break. The equivalent is \\x0a and \\cJ.
\rMatches a return sign. The price is equal to \\x0d and \cm.
\sMatches any blank characters, including spaces, tabs, page breaks, etc. The value is equal to [\f\r\t\v].
\SMatches any non-empty character. The value is equal to [^f\r\t\v].
\tMatches a tab. Equivalent to \\x09 and \cI.
\vMatches a vertical tab. The price is equal to \\x0b and \\cK.
\wMatches any word characters that include underlineds. The equivalent is "[A-Za-z0-9 ]".
\WMatches any non-word characters. The equivalent is "[^A-Za-z0-9 ]".
\xnMatchn n, of which n is the hexadecimal conversion value. Hexadecimal conversion value must be long. For example, " \\x41 " matches " A ". " \\x041 " equals "\x04&1 ". Regular expressions can be coded using ASCII.
\numMatches the num, of which num is a positive integer. A reference to the matching obtained. For example, "(...................
\nMarks a octal conversion value or a backward reference. If you have at least n acquired subexpressions before \n, then n is quoted backwards. Otherwise, if n is an octal conversion value (0-7).
\nmMarks a octal conversion value or a backward reference. If at least nm obtained a subexpression before \nm, then \n is quoted backwards. If \nm acquired at least n before, \n is quoted backwards. If the conditions are not met, \nm will match the octal conversion value nm if both n and m are octal (0-7).
\nmlIf n is octal (0-3) and both m and l are octal (0-7), the octal conversion value is matched to nml.
\unmatches n, where n is a Unicode character expressed in four hexadecimals. For example, \u00A9 matches the copyright symbol (?).
Ferramentas recentes: