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: 2020-03-10
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 | `[[:print:]]` | Any printable character including spaces |
34 | `[^abc]`      | Any character except `a`, `b` or `c`     |
35
36 ### Anchors
37
38 | Pattern | Description             |
39 | ------- | ----------------------- |
40 | `\G`    | Start of match          |
41 | `^`     | Start of string         |
42 | `$`     | End of string           |
43 | `\A`    | Start of string         |
44 | `\Z`    | End of string           |
45 | `\z`    | Absolute end of string  |
46 | `\b`    | A word boundry          |
47 | `\B`    | Non-word boundry        |
48 | `^abc`  | Start with `abc`        |
49 | `abc$`  | End with `abc`          |
50
51 ### Escaped characters
52
53 | Pattern    | Description                            |
54 | ---------- | -------------------------------------- |
55 | `\. \* \\` | Escape special character used by regex |
56 | `\t`       | Tab                                    |
57 | `\n`       | Newline                                |
58 | `\r`       | Carriage return                        |
59
60 ### Groups
61
62 | Pattern   | Description                    |
63 | --------- | ------------------------------ |
64 | `(abc)`   | Capture group                  |
65 | `(a|b)`   | Match `a` or `b`               |
66 | `(?:abc)` | Match `abc`, but don't capture |
67
68
69 ### Quantifiers
70
71 | Pattern  | Description           |
72 | -------- | --------------------- |
73 | `a*`     | Match 0 or more       |
74 | `a+`     | Match 1 or more       |
75 | `a?`     | Match 0 or 1          |
76 | `a{5}`   | Match exactly 5       |
77 | `a{,3}`  | Match up to 3         |
78 | `a{3,}`  | Match 3 or more       |
79 | `a{1,3}` | Match between 1 and 3 |
80
81 ### Lookahead & Lookbehind
82
83 | Pattern      | Description                               |
84 | ---          | ---                                       |
85 | `a(?=b)`     | Match `a` in `baby` but not in `bay`      |
86 | `a(?!b)`     | Match `a` in `Stan` but not in `Stab`     |
87 | ---          | ---                                       |
88 | `(?<=a)b`    | Match `b` in `crabs` but not in `cribs`   |
89 | `(?<!a)b`    | Match `b` in `fib` but not in `fab`       |