×
Community Blog How to Filter Queries Using Regular Expressions

How to Filter Queries Using Regular Expressions

This article explains how to filter queries using Regular Expressions.

By Alain Francois

What Are Regular Expressions?

Regular Expressions are special characters that help search data and match complex patterns using placeholder characters called operators, such as Joker or _Wild-card_. They are commonly called regex (regular expression) and represent patterns of character strings. They can be found in many languages, such as PHP, MySQL, and JavaScript.

Common Regular Expressions

Dome commons regular expressions that can be used to match some of your filter expectations are listed below:

  • ^ matches the beginning of a line or string
  • $ matches the end of a line or string
  • * matches zero (0) or more occurrences of an element. You should also be aware that the result can show those with the specified element. This means a* can give no occurrence, such as aa or aeb.
  • ? matches zero (0) or one (1) occurrence
  • . (period) matches any character
  • + matches one (1) or more occurrences of an element. This means a+ should be a, aa, or aaa. You can see that it is different from the *.
  • left|right will succeed if the right or left pattern side matches.
  • [] is used to match one of the characters in the brackets. You can specify a range when using - inside the brackets.

    • [a-z]: matches a single lowercase letter
    • [A-Z] matches a single uppercase letter
    • [0-9]: matches any number
  • () It is used to match or search for a set of regular expressions.

Prerequisites

This guide uses an Alibaba Cloud ECS Linux instance. If you don’t have an Alibaba Cloud account already, you can sign up for a Free Trial.

Filtering Using Brackets

You can match a group of characters by enclosing them inside the brackets.

Let's filter all the lines containing mai or man in the /etc/passwd file:

  • grep:
$ grep '^daemon' /etc/passwd
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
  • sed:
$ sed -n '/ma[in]/p' /etc/passwd
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin

We can also filter all the lines containing syn, syo, syp down to syt:

  • grep:
$ grep 'sy[n-t]' /etc/passwd

1

  • sed:
$ sed -n '/sy[n-t]/p' /etc/passwd

2

Filtering Using Anchors

Anchors are meta-characters that allow you to specify where in the line the match must be found.

Let's try to list all the lines starting with the string daemon in the /etc/passwd file:

  • grep:
$ grep '^daemon' /etc/passwd

3

  • sed:
$ sed -n '/^daemon/p' /etc/passwd

4

Now, let’s list all the lines ending with the string bash in the /etc/passwd file:

  • grep:
$ grep 'bash$' /etc/passwd

5

  • sed:
$ sed -n '/bash$/p' /etc/passwd

6

Filtering Using Alternation, Single Character, or Quantifier

You can use the . operation to match any single character:

$  sed -n '/sys..g/p' /etc/passwd
syslog:x:104:110::/home/syslog:/usr/sbin/nologin

You can also filter all the lines containing all occurrences of specified words or strings:

$ grep -E 'syslog|bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
franck:x:1000:1000:,,,:/home/franck:/bin/bash

Using * we can match up to zero or more occurrences of the character of the string:

# grep 'mes*age' /etc/passwd
messagebus:x:103:106::/nonexistent:/usr/sbin/nologin
0 0 0
Share on

Alibaba Cloud Community

1,029 posts | 252 followers

You may also like

Comments