1 2 3 4 5 6 7 8 9

Regular Expressions

(for pattern matching)

strcmp (char*, char*)can be used for exact matching of strings, but often we want to find substrings that conform to certain constraints.

Regular expressions specify a pattern to be sought in an array of characters. We can define our regular expression and then ask the system if particular strings conform to it.

Regular expressions are found throughout the UNIX environment, both at the command line (through grep--Get Regular ExPression), and through system calls from within user programs.

A few single character-matching codes

^

$

.

c

denotes the start of a line

denotes the end of the line

and single character

Any character; use \cif special (like, ^,

&,.)

[abc]a, b or c are matched

[d-k]any of d, e, f, g, h, i, j or k are matched

[^xyz]

any character not in the set {xyz}

4 February, 1998

6

Copyright University of Alberta