inharest / hello-world

Just another repository
0 stars 0 forks source link

Regex #22

Open inharest opened 7 years ago

inharest commented 7 years ago

Syntax

Meta characters

Metacharacter Description
| Separates alternate possibilities.
^ Matches the beginning of a line or string.
$ Matches the end of a line or string.
[ ] Matches every character inside brackets.
. Matches any single character.
\w Matches an alphanumeric character.
\s Matches a whitespace character.
\d Matches a digit.
\b Matches a word boundary.

Antisense characters

Metacharacter Description
[^ ] Matches every character except inside brackets.
\W Matches a non-alphanumeric character.
\S Matches anything but a whitespace.
\D Matches a non-digit.
\B Matches a non-word boundary.

Escape characters

Metacharacter Description
\f Matches a form feed (U+000C).
\n Matches a line feed (U+000A).
\r Matches a carriage return (U+000D).
\t Matches a tab (U+0009).
\v Matches a vertical tab (U+000B).
\xhh Matches the character with the code hh (two hexadecimal digits).
\uhhhh Matches the character with the code hhhh (four hexadecimal digits).

Greed

Metacharacter Description
? Matches the preceding pattern element zero or one time.
* Matches the preceding pattern element zero or more times.
+ Matches the preceding pattern element one or more times.
{N} Matches the preceding pattern element N times.
{N, } Matches the preceding pattern element N or more times.
{M, N} Matches the preceding pattern element at least M times, but not more than max times.

Laziness

Metacharacter Description
? Modifies the *, +, ? or {M,N}'d regex that comes before to match as few times as possible.
?? Matches ? time, match as few times as possible.
*? Matches * time, match as few times as possible.
+? Matches + time, match as few times as possible.
{M, N}? Matches {M, N} time, match as few times as possible.

Grouping

Metacharacter Description
( ) Groups a series of pattern elements to a single element.
(exp) Matches exp and remembers the match.
(?:exp1) Matches exp but does not remember the match.
exp1(?=exp2) Matches exp1 only if exp1 is followed by exp2. This is called a lookahead.
exp1(?!exp2) Matches exp1 only if exp1 is not followed by exp2. This is called a negated lookahead.

Modifier

Flag Description
g Global search.
i Case-insensitive search.
m Multi-line search.

References

Regular expression - wikipedia Regular Expressions - MDN Regular expressions 正则表达式30分钟入门教程

Visualize

Regulex Regexper Debuggex