[el7_blog]
/dev/urandom

regular expression primer

A regular expression is a search pattern that allows you to look for specific text in an advanced and flexible way.

Using Line Anchors
The type of regular expression that specifies where in a line of output the result is expected is known as a line anchor. To show lines that start with text you are looking for, you can use the regular expression ^. For instance, grep ^root /etc/passwd indicates you are looking only for lines that have root at the beginning.


grep '^root' /etc/passwd
root:x:0:0:root:/root:/bin/bash

Another regular expression that relates to the position of the specific text in a specific line is $, which states the lines end with some text. For instance, grep 'halt$' /etc/passwd indicates you are looking only for lines that have halt at the end.


grep 'halt$' /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt

Escaping W/Regular Expressions
When using regular expressions it’s a good idea to use escaping to prevent shell interpretation. To prevent shell interpretation from ever happening, put the regular expression between quotes. For example, grep '^root' /etc/passwd instead of grep ^root /etc/passwd.

Wildcards and Multipliers
To start with there is a . regular expression. This is used as a wildcard character to look for one specific character. So, the regular expression r.t would match the strings rat, rot, and rut.

In some cases, you might want to be more specific about the characters you are looking for. For instance, the regular expression r[aou]t matches the strings rat, rot, and rut as well.

Another useful regular expression is the multiplier *. This matches zero or more of the previous character.

If you know exactly how many of the previous character you are looking for, you can specify a number, as in re\{2\}d, which would match red as well as reed. The last regular expression that is useful to know about is ?, which matches zero or one of the previous character.

Significant Regular Expressions

Regular Expression Use
^text Line starts with text.
text$ Line ends with text.
. Wildcard, matches any single character.
[abc] Matches a, b, or c.
* Matches 0 to an infinite number of the previous character.
\{2\} Matches exactly 2 of the previous character.
\{1,3\} Matches a minimum of 1 and a maximum of 3 of the previous character.
colou?r Match 0 or 1 of the previous character. This makes the previous character optional, which in this example would match both color and colour