quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bullet_green_ball.gif What is regular expression?

A regular expression is a string of characters which tells the searcher which string (or strings) you are looking for. The following explains the format of regular expressions in detail.

   

space.gif

As with most human languages the regex language has many dialects; regexes written for perl aren't automatically suited for sed, awk or grep, to name just a few standard UNIX tools.

   

space.gif

I've chosen to write all the regexes in this tutorial in the POSIX dialect. This because POSIX is slowly winning terrain in the world of regexes, and because a fair amount of dialects are similar to it (well, actually it's the other way around). But this doesn't mean I'll be covering all the features of the POSIX 1003.2 regular expression standard. Another reason for using the POSIX dialect as opposed to the Perl dialect is because the Perl documentation does a much better job of explaining the Perl dialect than I ever will. Also, this way you won't be locked into any particular tool's regex extensions. In a way, the POSIX dialect can be considered the greatest common denominator.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Simple Regular Expressions

In its simplest form, a regular expression is just a word or phrase to search for. For example,

   

space.gif

Verilog

   

space.gif

would match any subject with the string "Verilog" in it, or which mentioned the word "Verilog" in the subject line. Thus, subjects with "Verilog", "SystemVerilog" or "iVerilog" would all be matched, as would a subject containing the phrases "Verilog Simulator" or "SystemVerilog Book." Here are some more examples:

   

space.gif

  • sim :Finds any subject with the string "sim" in its name, or which mentions sim (or simulation or gatesim or Verilog simulation) in the subject line.
  • gate : Finds any subject with the string "gate" in its name or contents. Subjects with "gate", "gatelevel or "and_gate" are found, as well as subjects containing the words "gate sim" or "udp gates".
   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Metacharacters

Some characters have a special meaning to the searcher. These characters are called metacharacters. Although they may seem confusing at first, they add a great deal of flexibility and convenience to the searcher.

   

space.gif

Charactor

Description

^

Start of line

$

End of line

.

Any any other character

*

Match 0 or more of the preceeding character.

?

Match 0 or 1 occurance of the previous character.

+

Match 1 or more occurances of the previous character.

\t

The Tab character

\n

The carriage return

\s

Match any whitespace (space, tab, etc)

\d

Match any digit (same as [0-9]).

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Structure of a Regular Expression

There are three important parts to a regular expression. Anchors are used to specify the position of the pattern in relation to a line of text. Character Sets match one or more characters in a single position. Modifiers specify how many times the previous character set is repeated. A simple example that demonstrates all three parts is the regular expression "^#*." The up arrow is an anchor that indicates the beginning of the line. The character "#" is a simple character set that matches the single character "#." The asterisk is a modifier. In a regular expression it specifies that the previous character set can appear any number of times, including zero.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Unix regular expressions

Regular expression syntax is now defined as obsolete by POSIX, but is still widely used for the purposes of backwards compatibility. Most regular-expression-aware Unix utilities, for example grep and sed, use it by default.

   

space.gif

In this syntax, most characters are treated as literals—they match only themselves ("a" matches "a", "(bc" matches "(bc", etc). The exceptions are called metacharacters:

   

space.gif

.

Matches any single character

[ ]

Matches a single character that is contained within the brackets. For example, [abc] matches "a", "b", or "c". [a-z] matches any lowercase letter. These can be mixed, [abcq-z] matches a, b, c, q, r, s, t, u, v, w, x, y, z, and so does [a-cq-z].

'-'

This character should be literal only if it is the last or the first character within the brackets, [abc-] or [-abc]. To match an '[' or ']' character, the easiest way is to make sure the closing bracket is first in the enclosing square brackets, [][ab] matches ']', '[', 'a' or 'b'.

[^ ]

Matches a single character that is not contained within the brackets. For example, [^abc] matches any character other than "a", "b", or "c". [^a-z] matches any single character that is not a lowercase letter. As above, these can be mixed.

^

Matches the start of the line (or any line, when applied in multiline mode)

$

Matches the end of the line (or any line, when applied in multiline mode)

( )

Defines a "marked subexpression". What the enclosed expression matched can be recalled later. See the next entry, \n. Note that a "marked subexpression" is also a "block". Note that this is not found in some instances of regex.

\n

Where n is a digit from 1 to 9; matches what the nth marked subexpression matched. This construct is theoretically irregular and has not been adopted in the extended regular expression syntax.

*

* A single character expression followed by "*" matches zero or more copies of the expression. For example, "[xyz]*" matches "", "x", "y", "zx", "zyx", and so on.

* \n*

Where n is a digit from 1 to 9, matches zero or more iterations of what the nth marked subexpression matched. For example, "(a.)c\1*" matches "abcab" and "abcabab" but not "abcac".

{x,y}

Match the last "block" at least x and not more than y times. For example, "a\{3,5\}" matches "aaa", "aaaa" or "aaaaa". Note that this is not found in some instances of regex.

   

space.gif

Note that particular implementations of regular expressions interpret backslash differently in front of some of the metacharacters. For example, egrep and Perl interpret unbackslashed parentheses and vertical bars as metacharacters, reserving the backslashed versions to mean the literal characters themselves. Old versions of grep did not support the alternation operator "|".

   

space.gif

Examples

   

space.gif

  • ".at" matches any three-character string like hat, cat or bat
  • "[hc]at" matches hat and cat
  • "[^b]at" matches all the matched strings from the regex ".at" except bat
  • "^[hc]at" matches hat and cat but only at the beginning of a line
  • "[hc]at$" matches hat and cat but only at the end of a line
   

space.gif

Since many ranges of characters depends on the chosen locale setting (e.g., in some settings letters are organized as abc..yzABC..YZ while in some others as aAbBcC..yYzZ) the POSIX standard defines some classes or categories of characters as shown in the following table.

   

space.gif

POSIX

class

similar to meaning

[:upper:]

[A-Z]

uppercase letters

[:lower:]

[a-z]

lowercase letters

[:alpha:]

[A-Za-z]

upper- and lowercase letters

[:alnum:]

[A-Za-z0-9]

digits, upper- and lowercase letters

[:digit:]

[0-9]

digits

[:xdigit:]

[0-9A-Fa-f]

hexadecimal digits

[:blank:]

[ \t]

space and TAB

[:space:]

[ \t\n\r\f\v]

blank characters

[:cntrl:]

control characters

[:graph:]

[^ \t\n\r\f\v]

printed characters

[:print:]

[^\t\n\r\f\v]

printed characters and space

   

space.gif

Example: [[:upper:]ab] should only match the uppercase letters and lowercase 'a' and 'b'.

   

space.gif

It is generally agreed that [:print:] consists of [:graph:] plus the space character. However, in PERL regular expressions [:print:] matches [:graph:] union [:space:].

   

space.gif

An additional non-POSIX class understood by some tools is [:word:], which is usually defined as [:alnum:] plus underscore. This reflects the fact that in many programming languages these are the characters that may be used in identifiers. The editor vim further distinguishes word and word-head classes (using the notation \w and \h) since in many programming languages the characters that can begin an identifier are not the same as those that can occur in other positions.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Anchor Characters: ^ and $

The character "^" is the starting anchor, and the character "$" is the end anchor. The regular expression "^A" will match all lines that start with a capital A. The expression "A$" will match all lines that end with the capital A.

   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com