sábado, 4 de abril de 2020

[EN] Regular Expressions

Hello everyone!

How do you carry the lockdown? At the moment nothing bad, taking advantage of the time to do things that before didn't have enough time to do.

One of these things is to study; I have finished with a very interesting 'sed' and 'awk' course, and I wanted to share some notes on regular expressions, to see what you think.

‘\b[Cc]olou?r\b’
\b :: boundaries, limits the word
? :: optional inclusion of this character
\. :: Exactly one single character
this example matches Color, color, Colour, colour

Anchors
'^' :: start of a string
'$' :: end of a string

Ranges
[] :: denote the ranges
'[A-Za-z]' :: any letter
'[0-9]' :: any number; it could be represented as \d
'[a-z_]' :: lower case character and underscore character
'[349]' :: matches number 3, number 4 and number 9; it would match 34, 49 or 349 because include those numbers
'[^4]' :: matches anything BUT 4
'[Ss]erver' :: matches Server and server

Boundaries and strings
\s :: any white space character (space, line return, tap) > \S :: NOT looking for a white space character
\b :: word boundary (may include hyphen as word separator) >  \B :: NOT looking for a word boundary
'\ssytem' :: Matches "file system"
'\bsystem' :: Matches "file system" and "file-system"
'\bpop[0-9]\b' :: Matches pop2 and pop3 but not pop3s (from /etc/services file)
'\bpop[0-9]\B' :: Not matches pop2 and pop3 but matches pop3s (from /etc/services file)

Quantifiers
'u*' :: Matches u zero or more times
'u?' :: Matches u zero or once only (optional)
'u+' :: Matches u once or more times
'u{3}' :: Matches uuu (u 3 times)


'^\s*#' :: Matches all kind of commented lines: with no space, with spaces and with tabs
'start\s*end' :: Matches all independently the spaces between 'start' and 'end'
'start\s?end' :: Matches 'start end' and 'startend'
'start\s{2}end' :: Matches the option with 2 spaces between 'start and 'end'
'[a-z]{2}[0-9]{1,2}' :: Matches a postcode [ab12 7af]; {1,2} means that it could happen once or twice

grep -E :: takes extended regular expressions :: egrep. RE are extended when they use {}, but not when they use []

 



No hay comentarios: