Hands on with RegEx
Kyle Waters
Venture Data LLC
Word Characters
- \w Match any word character
- a-z A-Z 0-9
Wildcard
- .(period) Matches every character
Line Beginnings/Endings
- ^ Matches the start of a line
- $ Matches the end of a line
- ^\d\.
Callback
- () Stores a match
- Useful for replacements
- ^(\d\.)
-  \1
Repeaters
- * matches 0 or more
- + matches 1 or more
- ? matches 0 or 1
- ^\d+\.
- ^\s*\d+
- ^\d+\.?
Either Or
- Place characters in [] to match either or
- Can be used as ranges
- [\s\w]+
- [1-3]+
- [AF]M
Either Or Again
- Use Pipe(|) to match various strings
- (Brown|Green|Blue)\s+Ajah
Greedy
- Questions marks make other repeaters less greedy
- -.*\.
- -.*?\.
Examples
- ^(\s*\d+)\.?\s+([\s\w]+) with \1. <a>\2</a>
- >(\w+) with href="\1">\1
- ^\s*\d+\.?\s+(.*)$ with <li>\1</li>
Interpolation
- \U\u\L\l
- ((Brown|Green|Blue)\s+Ajah) with \l\1
Repeat Count
- use {} to match a specific number(or range)
- \w{3}
- \w{3-5}
Look Ahead
- h(?!\.)
- \w+\s+\w+\s+(?=-)
Where
- Text Editors
- Word Processor
- Programming Languages
- grep/sed
- Faker
- salt