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

https://regex101.com/r/5OD29b/1

[]

Or any of the character NOT in the brackets

https://regex101.com/r/3In2a4/1

Or in a range of characters

https://regex101.com/r/7xR0h8/1

If you need to use literal - or ^ in the brackets:

https://regex101.com/r/Y845ZY/1

The dot means "Any character"

https://regex101.com/r/P8glif/1

. |

The pipe can assimilated to OR:

https://regex101.com/r/m5kG9b/1

?, +, *

The plus means the preceding item must be present at least once

https://regex101.com/r/pXaQ60/1

The asterisk means the preceding item may be present 0 or more times

https://regex101.com/r/tGnNNT/1

The question mark allow preceding item to be present or not:

https://regex101.com/r/T3975z/1

{}

{0,3} means  the preceding item must be present at most 3 times

https://regex101.com/r/fW3aks/1

{3} means  the preceding item must be present exactly 3 times

https://regex101.com/r/IoSf0S/1

{3,} means  the preceding item must be present at least 3 times

https://regex101.com/r/4OuLXd/1

{2,3} means  the preceding item must be present 2 or 3 times

https://regex101.com/r/42B2dQ/1

() 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

Made with Slides.com