Regulære udtryk syntaks hurtig reference

Regular expressions, also known as regex, are a type of text pattern typically used to search, replace, and control text. They mainly include letters from a to z as well as some special metacharacters. The application scope of regular expressions is very extensive, originally popularized by Unix and later widely used in Scala, PHP, C#, Java, C++, Objective-C, Perl, Swift, VBScript, JavaScript, Ruby, and Python, among others. Learning regular expressions is essentially about learning a very flexible logical thinking process that allows control over strings through simple and quick methods.
Regex Character Description
\ Escapes the next character to be treated as a special character, a literal character, a backreference, or an octal escape character. For example, “n" matches the character "n". "\n" matches a newline character. The sequence "\\" matches "\" and "\(" matches "(".
^ Matches the start of the input string. If the Multiline property of the RegExp object is set, ^ also matches positions following "“\n" or "\r".
$ Matches the end of the input string. If the Multiline property of the RegExp object is set, $ also matches positions preceding "“\n" or "\r".
* Matches the preceding subexpression zero or more times. For example, zo* can match "“z" and "zoo". * is equivalent to {0,}.
+ Matches the preceding subexpression one or more times. For example, "“zo+" can match "zo" and "zoo", but cannot match "z". + is equivalent to {1,}.
? Matches the preceding subexpression zero or one time. For example, "“do(es)?" can match "does" or "do" in "does". ? is equivalent to {0,1}.
{n} n is a non-negative integer. Matches exactly n times. For example, "“o{2}" cannot match "Bob"'s "o", but can match two o's in "food".
{n,} n is a non-negative integer. Matches at least n times. For example, "“o{2,}" cannot match "Bob"'s "o", but can match all o's in "foooood". "o{1,}" is equivalent to "o+". "o{0,}" is equivalent to "o*".
{n,m} n and m are non-negative integers, with n <= m. Matches 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?". Be aware that there should be no spaces between the comma and the two numbers.
? When this character follows any other quantifiers (*, +, ?, {n}, {n,}, {n,m}), the matching pattern is non-greedy. The non-greedy mode matches as few characters as possible, whereas the default greedy mode matches as many characters as possible. For example, for the string "“oooo", "o+?" will match a single "o", while "o+" will match all "o".
. Matches any single character except for "“\n". To match any character including "“\n", use a pattern like "(.|\n)".
(pattern) Matches the pattern and captures the match. The captured match can be retrieved from the Matches collection produced; in VBScript, this is done using the SubMatches collection, while in JScript it uses the $0…$9 properties. To match parenthesis characters, use "“\(" or "\)".
(?:pattern) Matches the pattern but does not capture the result, meaning this is a non-capturing match and will not be stored for later use. This is useful when using the or character "“(|)" to combine various parts of a pattern. For example "industr(?:y|ies)" is a more concise expression than "industry|industries".
(?=pattern) Positive lookahead assertion, matches a string only at the start of a string that matches the pattern. This is a non-capturing match, meaning this match does not need to be stored for later use. For example, "“Windows(?=95|98|NT|2000)" can match "Windows" in "Windows2000", but cannot match "Windows" in "Windows3.1". Lookahead does not consume characters, meaning after a match occurs, the next search for a match begins immediately after the last successful match.
(?!pattern) Negative lookahead assertion, matches the search string only at the start of a string that does not match the pattern. This is also a non-capturing match. For example, "“Windows(?!95|98|NT|2000)" can match "Windows" in "Windows3.1", but cannot match "Windows" in "Windows2000". Lookahead doesn’t consume characters as well.
(?<=pattern) Positive lookbehind assertion, akin to positive lookahead assertion but in the opposite direction. For example, "“(?<=95|98|NT|2000)Windows" can match "Windows" in "2000Windows", but cannot match "Windows" in "3.1Windows".
(?<!pattern) Negative lookbehind assertion, analogous to negative lookahead assertion but in the opposite direction. For example, "“(?<!95|98|NT|2000)Windows" can match "Windows" in "3.1Windows", but cannot match "Windows" in "2000Windows".
x|y Matches x or y. For example, "“z|food" can match "z" or "food". "(z|f)ood" matches "zood" or "food".
[xyz] Character class. Matches any one of the included characters. For example, "“[abc]" can match the "a" in "plain".
[^xyz] Negation character class. Matches any character not included. For example, "“[^abc]" can match the "p" in "plain".
[a-z] Character range. Matches any character in the specified range. For example, "“[a-z]" can match any lowercase letter character from "a" to "z".
[^a-z] Negation character range. Matches any character not within the specified range. For example, "“[^a-z]" can match any character not within the range from "a" to "z".
\b Matches a word boundary, which is the position between a word and whitespace. For example, "“er\b" can match "never"'s "er", but cannot match "verb"'s "er".
\B Matches a non-word boundary. "“er\B" can match "verb"'s "er", but cannot match "never"'s "er".
\cx Matches the control character specified by x. For example, \cM matches a Control-M or carriage return character. x must be one of A-Z or a-z. Otherwise, c is taken as a literal "c" character.
\d Matches a digit character. Equivalent to [0-9].
\D Matches a non-digit character. Equivalent to [^0-9].
\f Matches a form feed character. Equivalent to \x0c and \cL.
\n Matches a newline character. Equivalent to \x0a and \cJ.
\r Matches a carriage return character. Equivalent to \x0d and \cM.
\s Matches any whitespace character, including space, tab, form feed, etc. Equivalent to [ \f\n\r\t\v].
\S Matches any non-whitespace character. Equivalent to [^ \f\n\r\t\v].
\t Matches a tab character. Equivalent to \x09 and \cI.
\v Matches a vertical tab character. Equivalent to \x0b and \cK.
\w Matches any word character, including underscore. Equivalent to "“[A-Za-z0-9_]".
\W Matches any non-word character. Equivalent to "“[^A-Za-z0-9_]".
\xn Matches n, where n is a hexadecimal escape value. The hexadecimal escape value must be exactly 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. Used for referencing captured matches. For example, "“(.)\1" matches two consecutive identical characters.
\n Indicates an octal escape value or a backreference. If n has at least n captured subexpressions before it, then n is a backreference. Otherwise, if n is an octal digit (0-7), then n is an octal escape value.
\nm Indicates an octal escape value or a backreference. If nm has at least nm captured subexpressions before it, then nm is a backreference. If nm has at least n captured, then n is a backreference followed by the literal m. If neither condition is met, if both n and m are octal digits (0-7), then nm matches the octal escape value nm.
\nml If n is an octal digit (0-3), and both m and l are octal digits (0-7), then matches the octal escape value nml.
\un Matches n, where n is a Unicode character represented by four hexadecimal digits. For example, \u00A9 matches the copyright symbol (©).
Common Regular Expressions
Username /^[a-z0-9_-]{3,16}$/
Password /^[a-z0-9_-]{6,18}$/
Password 2 (?=^.{8,}$)(?=.*\d)(?=.*\W+)(?=.*[A-Z])(?=.*[a-z])(?!.*\n).*$ (Must contain digits, uppercase letters, lowercase letters, and punctuation, with all four types required and at least 8 characters long)
Hexadecimal Value /^#?([a-f0-9]{6}|[a-f0-9]{3})$/
Email /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
/^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/ or \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
URL /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ or [a-zA-z]+://[^\s]*
IP Address /((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)/
/^(?:(?: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]?)$/ or ((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)
HTML Tags /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/ or <(.*)(.*)>.*<\/\1>|<(.*) \/>
Remove Code\\ Comments (?<!http:|\S)//.*$
Match Double Byte Characters (including Chinese characters) [^\x00-\xff]
Chinese Characters (Characters) [\u4e00-\u9fa5]
Range of Chinese Characters in Unicode Encoding /^[\u2E80-\u9FFF]+$/
Chinese and Full-width Punctuation (Characters) [\u3000-\u301e\ufe10-\ufe19\ufe30-\ufe44\ufe50-\ufe6b\uff01-\uffee]
Date (YYYY-MM-DD) (\d{4}|\d{2})-((0?([1-9]))|(1[1|2]))-((0?[1-9])|([12]([1-9]))|(3[0|1]))
Date (MM/DD/YYYY) ((0?[1-9]{1})|(1[1|2]))/(0?[1-9]|([12][1-9])|(3[0|1]))/(\d{4}|\d{2})
Time (HH:MM, 24-hour format) ((1|0?)[0-9]|2[0-3]):([0-5][0-9])
Mainland China Fixed Phone Numbers (\d{4}-|\d{3}-)?(\d{8}|\d{7})
Mainland China Mobile Numbers 1\d{10}
Mainland China Postal Codes [1-9]\d{5}
Mainland China ID Numbers (15 or 18 digits) \d{15}(\d\d[0-9xX])?
Non-negative Integers (positive integers or zero) \d+
Positive Integers [0-9]*[1-9][0-9]*
Negative Integers -[0-9]*[1-9][0-9]*
Integers -?\d+
Decimals (-?\d+)(\.\d+)?
Blank Lines \n\s*\r or \n\n(editplus) or ^[\s\S ]*\n
QQ Numbers [1-9]\d{4,}
Words not containing abc \b((?!abc)\w)+\b
Match Leading and Trailing Whitespace Characters ^\s*|\s*$
Common Editing
Below are some replacements for special Chinese characters (editplus)
^[0-9].*\n
^[^第].*\n
^[习题].*\n
^[\s\S ]*\n
^[0-9]*\.
^[\s\S ]*\n
<p[^<>*]>
href="javascript:if\(confirm\('(.*?)'\)\)window\.location='(.*?)'"
<span style=".[^"]*rgb\(255,255,255\)">.[^<>]*</span>
<DIV class=xs0>[\s\S]*?</DIV>

Regex Syntax Quick Reference - Common Regex Techniques and Syntax

Regular expressions are a powerful text processing tool, and this page provides a common regex syntax quick reference to help you quickly understand the basic syntax, modifiers, subexpressions, greedy and non-greedy modes, etc.

Basic Syntax of Regular Expressions

Regular expressions are rules for describing string patterns. Common basic syntax includes:

Common Regular Expression Modifiers

Modifiers in regular expressions are used to alter the matching rules. Common modifiers include:

Greedy vs Non-Greedy Modes

In regular expressions, quantifiers are greedy by default, meaning they want to match as many characters as possible. Non-greedy mode can be used by adding ? after the quantifier, as shown below:

Subexpressions and Grouping

Subexpressions in regular expressions are defined using parentheses (( )) and are commonly used to extract matching parts. Grouping is achieved through the "|" symbol. For example:

Quick Reference for Regular Expressions

The following is a quick reference for commonly used regular expressions:

Function Regular Expression
Match Digits \d
Match Non-Digits \D
Match Letters [a-zA-Z]
Match Any Character .
Match Whitespace Characters \s

Applications of Regular Expressions

Regular expressions are widely used in the following scenarios:

Tips for Learning Regular Expressions

Immediately utilize our regex syntax quick reference to enhance your efficiency in writing regex and improve your text processing abilities!

Din fodspor:
Vælg sprog