Many Linux tools can use Regular Expressions to find a pattern (searching files, text, etc.), so it’s important to learn how to create a RegExp.

Table of Contents

Where to test RegExp?

  • regex101 is an easy-to-use website where you can test your RegExp knowledge inserting a text and a regular expression to find a pattern.

RegExp patterns

Characters

  • In the following examples, [ ] in the “Text:” line means a match.
  • A RegExp usually is created in this way: /pattern/options: /potato/g.
  • Above pattern will look for all occurrences of potato.
  • If you remove g, it will only look for the first occurrence.
  • If you add i option, search will be case-insensitive.
    Pattern: /potato/
    Text: Potato
    
    Pattern: /potato/i
    Text: [Potato]
    
  • [abc] will look for any of the included characters. [^abc] will do the opposite.
    Pattern: /[eo]/g
    Text: H[e]ll[o] w[o]rld
    
  • [a-z] will look for any (lowercase) letter in the range.
    Pattern: /[a-z]/g
    Text: P[o][t][a][t][o]
    
    Pattern: /[a-zA-Z]/g
    Text: [P][o][t][a][t][o]
    
  • . will look for any character (except line terminators).
  • \w will look for any alphanumeric character and underscore (except accented letters). \W will do the opposite.
  • \d will look for digits. \D will do the opposite.
  • \s will look for any whitespace character. \S will do the opposite.
    Pattern: /\s/
    Text: Hello[ ]world
    

Positions

  • ^ will look at the beginning of the line.
    Pattern: /^[A-Z]/g
    Text: [H]ello World
    
  • $ will look at the end of the line.
    Pattern: /[a-z]$/g
    Text: Hello worl[d]
    
  • \b will look for a word boundary (place between a word character \w and a non-word character \W). \B will do the opposite.
    Pattern: /\b[a-z]/g
    Text: Hello [w]orld [a]nd [u]niverse
    

Quantifiers

  • + will look for one or more characters.
    Pattern: /[a-z]+/
    Text: 20 [potatos]
    
  • * will look for zero or more characters.
  • {2} will look for an specific number of characters, in this case two. ({1,3}: between one and three characters, {2,}: two or more).
    Pattern: /l{2}/
    Text: He[ll]o world
    
  • ? matches previous character one or zero times.
    Pattern: /ld?/g
    Text: He[l][l]o wor[l][d]
    

Groups

You can define groups of patterns using parenthesis (). You can then replace each pattern group separately.

Pattern: /([a-z]{4})\-([0-9]{3})/
Text: Some code: [[text]-[012]]

If you have any suggestion, feel free to contact me via social media or email.