OSDN Git Service

Regular updates
[twpd/master.git] / regexp.md
1 ---
2 title: regexp
3 category: Others
4 layout: 2017/sheet
5 weight: -1
6 authors:
7   - github: rizqyhi
8   - github: kolapsys
9   - github: samtrion
10 updated: 2019-11-14
11 description: |
12   Basic cheatsheets for regular expression
13 ---
14
15 ## RegExp
16 {: .-three-column}
17
18 ### Character classes
19
20 | Pattern  | Description                          |
21 | -------- | ------------------------------------ |
22 | `.`      | Any character, except newline        |
23 | `\w`     | Word                                 |
24 | `\d`     | Digit                                |
25 | `\s`     | Whitespace                           |
26 | `\W`     | Not word                             |
27 | `\D`     | Not digit                            |
28 | `\S`     | Not whitespace                       |
29 | `[abc]`  | Any of a, b, or c                    |
30 | `[a-e]`  | Characters between `a` and `e`       |
31 | `[1-9]`  | Digit between `1` and `9`            |
32 | `[^abc]` | Any character except `a`, `b` or `c` |
33
34 ### Anchors
35
36 | Pattern | Description             |
37 | ------- | ----------------------- |
38 | `\G`    | Start of match          |
39 | `^`     | Start of string         |
40 | `$`     | End of string           |
41 | `\A`    | Start of string         |
42 | `\Z`    | End of string           |
43 | `\z`    | Absolute end of string  |
44 | `\b`    | A word boundry          |
45 | `\B`    | Non-word boundry        |
46 | `^abc`  | Start with `abc`        |
47 | `abc$`  | End with `abc`          |
48
49 ### Escaped characters
50
51 | Pattern    | Description                            |
52 | ---------- | -------------------------------------- |
53 | `\. \* \\` | Escape special character used by regex |
54 | `\t`       | Tab                                    |
55 | `\n`       | Newline                                |
56 | `\r`       | Carriage return                        |
57
58 ### Groups
59
60 | Pattern   | Description                    |
61 | --------- | ------------------------------ |
62 | `(abc)`   | Capture group                  |
63 | `(a|b)`   | Match `a` or `b`               |
64 | `(?:abc)` | Match `abc`, but don't capture |
65
66
67 ### Quantifiers
68
69 | Pattern  | Description           |
70 | -------- | --------------------- |
71 | `a*`     | Match 0 or more       |
72 | `a+`     | Match 1 or more       |
73 | `a?`     | Match 0 or 1          |
74 | `a{5}`   | Match exactly 5       |
75 | `a{,3}`  | Match up to 3         |
76 | `a{3,}`  | Match 3 or more       |
77 | `a{1,3}` | Match between 1 and 3 |
78