2014-02-25 10:32:14 +00:00
|
|
|
---
|
2018-12-06 22:17:16 +00:00
|
|
|
title: regexp
|
|
|
|
category: Others
|
|
|
|
weight: -1
|
|
|
|
authors:
|
|
|
|
- github: rizqyhi
|
2019-10-05 01:59:58 +00:00
|
|
|
- github: izzergh
|
2019-10-24 12:28:41 +00:00
|
|
|
- github: kolapsys
|
2019-11-14 15:56:16 +00:00
|
|
|
- github: samtrion
|
2020-07-04 13:33:09 +00:00
|
|
|
updated: 2020-03-10
|
2018-12-06 22:17:16 +00:00
|
|
|
description: |
|
|
|
|
Basic cheatsheets for regular expression
|
2014-02-25 10:32:14 +00:00
|
|
|
---
|
|
|
|
|
2019-12-25 08:10:46 +00:00
|
|
|
## RegExp
|
2019-03-23 23:38:52 +00:00
|
|
|
{: .-three-column}
|
|
|
|
|
|
|
|
### Character classes
|
|
|
|
|
2020-03-10 15:28:27 +00:00
|
|
|
| Pattern | Description |
|
|
|
|
| ------------- | ---------------------------------------- |
|
|
|
|
| `.` | Any character, except newline |
|
|
|
|
| `\w` | Word |
|
|
|
|
| `\d` | Digit |
|
|
|
|
| `\s` | Whitespace |
|
|
|
|
| `\W` | Not word |
|
|
|
|
| `\D` | Not digit |
|
|
|
|
| `\S` | Not whitespace |
|
|
|
|
| `[abc]` | Any of a, b, or c |
|
|
|
|
| `[a-e]` | Characters between `a` and `e` |
|
|
|
|
| `[1-9]` | Digit between `1` and `9` |
|
|
|
|
| `[[:print:]]` | Any printable character including spaces |
|
|
|
|
| `[^abc]` | Any character except `a`, `b` or `c` |
|
2014-02-25 10:32:14 +00:00
|
|
|
|
2018-12-06 22:17:16 +00:00
|
|
|
### Anchors
|
|
|
|
|
2022-12-02 09:16:09 +00:00
|
|
|
| Pattern | Description |
|
|
|
|
| ------- | ---------------------- |
|
|
|
|
| `\G` | Start of match |
|
|
|
|
| `^` | Start of string \* |
|
|
|
|
| `$` | End of string \* |
|
|
|
|
| `\A` | Start of string |
|
|
|
|
| `\Z` | End of string |
|
|
|
|
| `\z` | Absolute end of string |
|
2024-03-21 21:53:26 +00:00
|
|
|
| `\b` | A word boundary |
|
|
|
|
| `\B` | Non-word boundary |
|
2022-12-02 09:16:09 +00:00
|
|
|
| `^abc` | Start with `abc` |
|
|
|
|
| `abc$` | End with `abc` |
|
|
|
|
|
|
|
|
For multiline patterns (`m` flag), `^` and `$` will act as start and end of line.
|
2018-12-06 22:17:16 +00:00
|
|
|
|
2019-03-23 23:38:52 +00:00
|
|
|
### Escaped characters
|
2018-12-06 22:17:16 +00:00
|
|
|
|
2019-03-23 23:38:52 +00:00
|
|
|
| Pattern | Description |
|
|
|
|
| ---------- | -------------------------------------- |
|
2018-12-06 22:17:16 +00:00
|
|
|
| `\. \* \\` | Escape special character used by regex |
|
2019-03-23 23:38:52 +00:00
|
|
|
| `\t` | Tab |
|
|
|
|
| `\n` | Newline |
|
|
|
|
| `\r` | Carriage return |
|
2018-12-06 22:17:16 +00:00
|
|
|
|
|
|
|
### Groups
|
|
|
|
|
2024-03-21 21:53:26 +00:00
|
|
|
| Pattern | Description |
|
|
|
|
| --------- | -------------------------------------------------------- |
|
|
|
|
| `(abc)` | Capture group |
|
|
|
|
| `(a|b)` | Match `a` or `b` |
|
|
|
|
| `(?:abc)` | Match `abc`, but don't capture |
|
|
|
|
| `\1` | Substituted with text matched of the 1st capturing group |
|
2019-10-14 08:27:55 +00:00
|
|
|
|
2018-12-06 22:17:16 +00:00
|
|
|
|
|
|
|
### Quantifiers
|
|
|
|
|
2019-03-23 23:38:52 +00:00
|
|
|
| Pattern | Description |
|
|
|
|
| -------- | --------------------- |
|
|
|
|
| `a*` | Match 0 or more |
|
|
|
|
| `a+` | Match 1 or more |
|
|
|
|
| `a?` | Match 0 or 1 |
|
|
|
|
| `a{5}` | Match exactly 5 |
|
|
|
|
| `a{,3}` | Match up to 3 |
|
|
|
|
| `a{3,}` | Match 3 or more |
|
2018-12-06 22:17:16 +00:00
|
|
|
| `a{1,3}` | Match between 1 and 3 |
|
2019-03-23 23:38:52 +00:00
|
|
|
|
2019-10-05 01:59:58 +00:00
|
|
|
### Lookahead & Lookbehind
|
|
|
|
|
|
|
|
| Pattern | Description |
|
|
|
|
| --- | --- |
|
|
|
|
| `a(?=b)` | Match `a` in `baby` but not in `bay` |
|
|
|
|
| `a(?!b)` | Match `a` in `Stan` but not in `Stab` |
|
|
|
|
| --- | --- |
|
|
|
|
| `(?<=a)b` | Match `b` in `crabs` but not in `cribs` |
|
|
|
|
| `(?<!a)b` | Match `b` in `fib` but not in `fab` |
|