└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Regex Cheatsheet 2 | 3 | ## Introduction 4 | Testing your regular expressions can be done in two ways: 5 | 1. **Create a text file** with some test paragraphs on a Unix machine, then use `egrep ` to see what matches and what doesn't. 6 | 2. **Use an online editor** like [regexr.com](https://regexr.com/). Add your text in the "Text" field and type your expressions in the "Expression" field. This method is highly recommended for ease of use. 7 | 8 | ## Charsets 9 | When searching for specific strings or patterns in a file or block of text, regular expressions (regex) come in handy. 10 | 11 | ### Basic Charsets 12 | - `[abc]`: Matches any occurrence of 'a', 'b', or 'c'. 13 | - `[abc]zz`: Matches 'azz', 'bzz', and 'czz'. 14 | 15 | ### Ranges 16 | - `[a-c]zz`: Same as above. 17 | - `[a-cx-z]zz`: Matches 'azz', 'bzz', 'czz', 'xzz', 'yzz', and 'zzz'. 18 | - `[a-zA-Z]`: Matches any single letter (lowercase or uppercase). 19 | - `file[1-3]`: Matches 'file1', 'file2', and 'file3'. 20 | 21 | ### Exclusions 22 | - `[^k]ing`: Matches 'ring', 'sing', '$ing', but not 'king'. 23 | - `[^a-c]at`: Matches 'fat' and 'hat', but not 'bat' or 'cat'. 24 | 25 | ### Note that 26 | 1. Charsets match any occurrence of the specified characters, not just strings. 27 | 2. Type characters in the same order as the questions to avoid confusion. 28 | 3. Efficient regex means: 29 | - Be specific: Use `[a-c]` instead of `[a-z]` if only matching 'a' to 'c'. 30 | - Don't be too specific: Use `[a-z]` for a broader range to avoid complexity. 31 | 32 | ## Examples 33 | 1. **Match characters `c`, `o`, `g`:** 34 | 35 | ```regex 36 | [cog] 37 | ``` 38 | 39 | 2. **Match words `cat`, `fat`, `hat`:** 40 | 41 | ```regex 42 | [cfh]at 43 | ``` 44 | 45 | 3. **Match words `Cat`, `cat`, `Hat`, `hat`:** 46 | ```regex 47 | [CcHh]at 48 | ``` 49 | 50 | 4. **Match filenames `File1`, `File2`, `file3`, `file4`, `file5`, `File7`, `file9`:** 51 | 52 | ```regex 53 | [Ff]ile[1-9] 54 | ``` 55 | 56 | 5. **Match all filenames except "File7":** 57 | 58 | ```regex 59 | [Ff]ile[^7] 60 | ``` 61 | 62 | 63 | ## Wildcards and Optional Characters 64 | - `.`: Matches any single character except the line break. 65 | - `?`: Makes the preceding character optional. 66 | 67 | ### Examples 68 | 1. **Match words `Cat`, `fat`, `hat`, `rat`:** 69 | 70 | ```regex 71 | .at 72 | ``` 73 | 74 | 2. **Match words `Cat`, `cats`:** 75 | 76 | ```regex 77 | [Cc]ats? 78 | ``` 79 | 80 | 3. **Match domain name `cat.xyz`:** 81 | 82 | ```regex 83 | cat\.xyz 84 | ``` 85 | 86 | 4. **Match domain names `cat.xyz`, `cats.xyz`, `hats.xyz`:** 87 | 88 | ```regex 89 | [ch]ats?\.xyz 90 | ``` 91 | 92 | 5. **Match 4-letter strings not ending with letters `n` to `z`:** 93 | 94 | ```regex 95 | ...[^n-z] 96 | ``` 97 | 98 | 6. **Match `bat`, `bats`, `hat`, `hats`, but not `rat` or `rats` (use the hat symbol)** 99 | 100 | ```regex 101 | [^r]ats? 102 | ``` 103 | 104 | ## Metacharacters and Repetitions 105 | ### Basic Metacharacters 106 | - `\d`: Matches a digit. 107 | - `\D`: Matches a non-digit. 108 | - `\w`: Matches an alphanumeric character. 109 | - `\W`: Matches a non-alphanumeric character. 110 | - `\s`: Matches a whitespace character. 111 | - `\S`: Matches everything else. 112 | 113 | ### Repetitions 114 | - `{n}`: Matches exactly `n` times. 115 | - `{n,m}`: Matches between `n` and `m` times. 116 | - `{n,}`: Matches `n` or more times. 117 | - `*`: Matches 0 or more times. 118 | - `+`: Matches 1 or more times. 119 | 120 | ### Examples 121 | 1. **Match word `catssss`:** 122 | 123 | ```regex 124 | cats{4} 125 | ``` 126 | 127 | 2. **Match words `Cat`, `cats`, `catsss`:** 128 | 129 | ```regex 130 | [Cc]ats* 131 | ``` 132 | 133 | 3. **Match sentences `regex go br`, `regex go brrrrrr`:** 134 | 135 | ```regex 136 | regex go br+ 137 | ``` 138 | 139 | 4. **Match filenames `ab0001`, `bb0000`, `abc1000`, `cba0110`, `c0000`:** 140 | 141 | ```regex 142 | [abc]{1,3}[01]{4} 143 | ``` 144 | 145 | 5. **Match filenames `File01`, `File2`, `file12`, `File20`, `File99`:** 146 | 147 | ```regex 148 | [Ff]ile\d{1,2} 149 | ``` 150 | 151 | 6. **Match all of the following folder names: `kali tools`, `kali tools`:** 152 | 153 | ```regex 154 | kali\s+tools 155 | ``` 156 | 157 | 7. **Match all of the following filenames: `notes~`, `stuff@`, `gtfob#`, `lmaoo!`:** 158 | 159 | ```regex 160 | \w{5}\W 161 | ``` 162 | 163 | 8. **Match the string in quotes (use the `*` sign and the `\s`, `\S` metacharacters): `"2f0h@f0j0%! a)K!F49h!FFOK"`:** 164 | 165 | ```regex 166 | \S*\s*\S* 167 | ``` 168 | 169 | 9. **Match every 9-character string (with letters, numbers, and symbols) that doesn't end in a "!" sign:** 170 | 171 | ```regex 172 | \S{8}[^!] 173 | ``` 174 | 175 | 10. **Match all of these filenames (use the `+` symbol): `.bash_rc`, `.unnecessarily_long_filename`, and `note1`:** 176 | 177 | ```regex 178 | \.?[\w_]+ 179 | ``` 180 | 181 | ## Start/End Patterns, Groups, and Either/Or 182 | ### Start/End Characters 183 | - `^`: Matches the start of a line. 184 | - `$`: Matches the end of a line. 185 | 186 | ### Groups and Either/Or 187 | - `(pattern)`: Defines a group. 188 | - `|`: Specifies an either/or pattern. 189 | 190 | ### Examples 191 | 1. **Match string starting with "Password:" followed by 10 characters excluding "0":** 192 | 193 | ```regex 194 | Password:[^0]{10} 195 | ``` 196 | 197 | 2. **Match "username: " at the beginning of a line:** 198 | 199 | ```regex 200 | ^username:\s 201 | ``` 202 | 203 | 3. **Match lines not starting with a digit:** 204 | 205 | ```regex 206 | ^\D 207 | ``` 208 | 209 | 4. **Match string `EOF$` at the end of a line:** 210 | 211 | ```regex 212 | EOF\$$ 213 | ``` 214 | 215 | 5. **Match sentences:** 216 | `I use vim` 217 | `I use nano` 218 | 219 | ```regex 220 | I use (nano|vim) 221 | ``` 222 | 223 | 7. **Match lines starting with `$` followed by a digit, another `$`, and non-whitespace characters:** 224 | 225 | ```regex 226 | \$\d\$\S+ 227 | ``` 228 | 229 | 8. **Match any IPv4 address:** 230 | 231 | ```regex 232 | (\d{1,3}\.){3}\d{1,3} 233 | ``` 234 | 235 | 9. **Match emails `hello@tryhackme.com`, `username@domain.com`, `dummy_email@xyz.com` and group the username and domain:** 236 | 237 | ```regex 238 | (\w+)@(\w+)\.com 239 | ``` 240 | 241 | ## Author 242 | This cheatsheet is created by **Purva Patel**. 243 | --------------------------------------------------------------------------------