Key Points
| Intro to Regular Expressions |
|
| Matching & Extracting |
|
| Matching & Substitution |
|
| Going further |
|
| Self-Multiple Choice Quiz |
|
| More Exercises |
|
Regular Expression Cheat Sheet
[]defines a range of characters..matches any character.\is used to escape the following character when that character is a special character. So, for example, a regular expression that found ‘.com’ would be\\.combecause.is a special character that matches any character.\dmatches any single digit.\wmatches any part of word character (equivalent to[A-Za-z0-9]).\smatches any space, tab, or newline.^asserts the position at the start of the line. So what you put after it will only match if they are the first characters of a line.$asserts the position at the end of the line. So what you put before it will only match if they are the last characters of a line.\badds a word boundary. Putting this either side of a stops the regular expression matching longer variants of words.*matches the preceding element zero or more times. For example,ab*cmatches ‘ac’, ‘abc’, ‘abbbc’, etc.+matches the preceding element one or more times. For example,ab+cmatches ‘abc’, ‘abbbc’ but not ‘ac’.?matches when the preceding character appears zero or one time.{VALUE}matches the preceding character the number of times define by VALUE; ranges can be specified with the syntax{VALUE,VALUE}.|means or.
Cheat Sheet PDFs
- Regular Expression Cheat Sheet (PDF) from qa-distiller.com. If you have access to a printer it would be useful to print this for reference during the workshop.
- More detailed Regular Expression Cheat Sheet (PDF) by Dave Child from Regex page on cheatography.com. This cheat sheet has more detail than you will need in this workshop.
Resources
- Online tools such as: Regxr, regex101, rexegper, myregexp, or whichever service you prefer.
- Test yourself with RegexCrossword.com or via the quiz and exercises in this lesson.
- You can visualize your regular expressions e.g. see
[Oo]rgani.evisulaized on Regexper.com - Learn Regular Expressions with simple, interactive exercises at https://regexone.com/
- Find a regex friend to consult when you need help.