Reg(ular )?Ex(pressions)?
A short introduction
What are they?!
Regular expressions are strings formatted using a special pattern notation that allow you to describe and parse text.
....
In other words, it's very similar to the following concepts:
Globs | SQL LIKE statements |
---|---|
/home/Photos/*.jpeg (matches all jpeg in the directory) /dev/eth? (matches all files starting with eth in that directory, as long as it's only one character extra) |
select * from users where firstname LIKE fabri% (Find fabrices and fabrizios) select * from patients where bloodgroup like "_-" (A, B, O rhesus -) |
Globs | SQL LIKE statements |
---|---|
/home/Photos/*.jpeg What about *.jpg? /dev/eth? What about /dev/eth14 or /dev/etha? |
select * from users where firstname LIKE Fabri% select * from patients where bloodgroup like "_-" What about AB- ? |
Limitations
Regex allows a more refined search, at the cost of a bit of complexity - you have to learn the language.
Good news, it's not that hard:
http://web.archive.org/web/20090209182018/http://immike.net/blog/2007/04/06/the-absolute-bare-minimum-every-programmer-should-know-about-regular-expressions/
^ marks the beginning of the text (usually, a line)
https://regex101.com/r/MpLLaN/1/
^$
$ marks the end of the text (usually, a line)
https://regex101.com/r/yUgHXY/1
This will match on any of the character in the brackets
[]
Or any of the character NOT in the brackets
Or in a range of characters
If you need to use literal - or ^ in the brackets:
The dot means "Any character"
. |
The pipe can assimilated to OR:
?, +, *
The plus means the preceding item must be present at least once
The asterisk means the preceding item may be present 0 or more times
The question mark allow preceding item to be present or not:
{}
{0,3} means the preceding item must be present at most 3 times
{3} means the preceding item must be present exactly 3 times
{3,} means the preceding item must be present at least 3 times
{2,3} means the preceding item must be present 2 or 3 times
() and \
Parenthesis are used to group patterns/characters together:
https://regex101.com/r/B7Iczg/1
\ are used to match any of the regex "keyword" characters:
https://regex101.com/r/o8l73C/1
Matching & replacing
Using parenthesis doesn't only allows you to create grouping, but also to refer to the found groups when replacing!
Let's head to IntelliJ
Use cases & examples
- Form validation
- More precise code search, both in browser and in editor
- Quick and dirty CSV to SQL statement
- Quickly fix some formatting issues
- Grep and find in command line; search for log, ...
- Splunk support them
Few links
- Regex golf! You'll learn a thing or two (and get frustrated a bit)
- regex101 (other similar tools exist, LMK if you know any better)
- More advanced stuff on regexes (we'll go through that on another presentation)
- find+ browser extension (Chrome & FF)
- Visualization for JS regexes
regexes
By fguery
regexes
- 138