regex-devrant

On Friday I took a break from Exercism and Scala. I checked my board, then I checked it again… RegEx was still there. I didn’t manage to write a post in time, so today I am giving an intro to regular expressions and how to use them in the terminal to save time. I had no idea where this was gonna get me but here are some findings and learnings.

What’s RegEx?

First click on Wikipedia gave me:

In computing, regular expressions provide a concise and flexible means for identifying strings of text of interest, such as particular characters, words, or patterns of characters. Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification.

Long story short,

  • With regexes you can match patterns of text. In most cases it’s used for parsing a big text and user-input validation.
  • They consist of numbers, letters ( ! Regexes are case sensitive) and some special characters - the metacharacters.
  • Metacharacters (e.g. ! . * ), if not escaped, have different super-powers in the expression. In order to escape a metacharacter and match it, you escape it using a “ \ “ . (e.g. \. , to match a dot)

  • You use the backslash with letters, too (more super-powers!) (e.g. \d , matches digits)

My goal is to not give a detailed post on regexes, but to get familiar with the concept and then use them with grep and sed .

First of all, I followed this interactive tutorial to get started. It is very easy to follow if you read carefully the description - I found them too long.

For the following examples, I created a file named example.txt:

It has been 1 month since I joined HC as an appentice.
I am trying to blog almost every day, but it is not that easy :/
At least I leave the office having learned something new, not always programming related ;)
Every day is unique here and full of smiles. You can always get help and "stuck" doesn't feel like a word I know anymore!!

FIY: I wrote this using vim :D

The sed command

The sed, as well as grep, are UNIX commands. With sed (Stream EDitor) you can perform search, replacements, insertions, deletions and other actions to manipulate the content of a file or multiple files. It is a non-interactive editor. While searching, I discoverd vim’s ex mode, which is less powerful but it can still replace sed at some tasks.
! IMPORTANT: some sequences don’t work with OSX sed, so do brew install gnu-sed and use gsed for functions like converting to upper/lowercase

//to convert all characters to lowercase --> \L (super-power)
$ gsed 's/./\L&/g' example.txt

it has been 1 month since i joined hc as an apprentice.
i am trying to blog almost every, but it is not that easy :/
but at least i leave the office with something new, not always programming related ;)
every day is uniq'ue' here and full of smiles. you can always get helped and "stuck" doesn't feel like a word i know anymore!!

fiy: i wrote this using vim :d

The grep command

It stands for Global Regular Expression Print. So with grep and regex you can search and find text patterns

// to find all lines with words that contain double vowels
$ grep -E "[AEIOUaeiou]{3}" GPL-3

//add --color to highlight matches
$ grep --color ".s" example.txt //would highlight all characters that are followed by an "s"

$ grep --color "s\." example.txt  //but here I am escaping ".", so it would match any "s" that is followed by a dot

All these are very easy examples, but baby steps is the plan! I will try to implement regex in my exercises, if possible, but I am not sure when it is gonna be needed.

Song of the day