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: izzergh
9   - github: kolapsys
10   - github: samtrion
11 updated: 2019-11-14
12 description: |
13   Basic cheatsheets for regular expression
14 ---
15
16 ## RegExp
17 {: .-three-column}
18
19 ### Character classes
20
21 | Pattern  | Description                          |
22 | -------- | ------------------------------------ |
23 | `.`      | Any character, except newline        |
24 | `\w`     | Word                                 |
25 | `\d`     | Digit                                |
26 | `\s`     | Whitespace                           |
27 | `\W`     | Not word                             |
28 | `\D`     | Not digit                            |
29 | `\S`     | Not whitespace                       |
30 | `[abc]`  | Any of a, b, or c                    |
31 | `[a-e]`  | Characters between `a` and `e`       |
32 | `[1-9]`  | Digit between `1` and `9`            |
33 | `[^abc]` | Any character except `a`, `b` or `c` |
34
35 ### Anchors
36
37 | Pattern | Description             |
38 | ------- | ----------------------- |
39 | `\G`    | Start of match          |
40 | `^`     | Start of string         |
41 | `$`     | End of string           |
42 | `\A`    | Start of string         |
43 | `\Z`    | End of string           |
44 | `\z`    | Absolute end of string  |
45 | `\b`    | A word boundry          |
46 | `\B`    | Non-word boundry        |
47 | `^abc`  | Start with `abc`        |
48 | `abc$`  | End with `abc`          |
49
50 ### Escaped characters
51
52 | Pattern    | Description                            |
53 | ---------- | -------------------------------------- |
54 | `\. \* \\` | Escape special character used by regex |
55 | `\t`       | Tab                                    |
56 | `\n`       | Newline                                |
57 | `\r`       | Carriage return                        |
58
59 ### Groups
60
61 | Pattern   | Description                    |
62 | --------- | ------------------------------ |
63 | `(abc)`   | Capture group                  |
64 | `(a|b)`   | Match `a` or `b`               |
65 | `(?:abc)` | Match `abc`, but don't capture |
66
67
68 ### Quantifiers
69
70 | Pattern  | Description           |
71 | -------- | --------------------- |
72 | `a*`     | Match 0 or more       |
73 | `a+`     | Match 1 or more       |
74 | `a?`     | Match 0 or 1          |
75 | `a{5}`   | Match exactly 5       |
76 | `a{,3}`  | Match up to 3         |
77 | `a{3,}`  | Match 3 or more       |
78 | `a{1,3}` | Match between 1 and 3 |
79
80 ### Lookahead & Lookbehind
81
82 | Pattern      | Description                               |
83 | ---          | ---                                       |
84 | `a(?=b)`     | Match `a` in `baby` but not in `bay`      |
85 | `a(?!b)`     | Match `a` in `Stan` but not in `Stab`     |
86 | ---          | ---                                       |
87 | `(?<=a)b`    | Match `b` in `crabs` but not in `cribs`   |
88 | `(?<!a)b`    | Match `b` in `fib` but not in `fab`       |