G.5 Examples of using regular expressions
The following table describes examples of using regular expressions.
Expression |
Meaning |
String specified as a regular expression |
Example pattern |
Match (Y) or No match (N) |
---|---|---|---|---|
string |
Matches lines containing the specified string. |
spring |
spring has come. |
Y |
winter-summer-autumn-spring |
Y |
|||
-----spring----- |
Y |
|||
^string |
Matches the specified string at the beginning of a line. |
^spring |
spring has come. |
Y |
winter-summer-autumn-spring |
N |
|||
-----spring----- |
N |
|||
string$ |
Matches the specified string at the end of a line. |
spring$ |
spring has come. |
N |
winter-summer-autumn-spring |
Y |
|||
-----spring----- |
N |
|||
^string$ |
Matches lines containing only the specified string. |
^spring$ |
spring has come. |
N |
winter-summer-autumn-spring |
N |
|||
spring |
Y |
|||
spring |
N |
|||
^$ |
Matches empty lines. |
^$ |
Y |
|
spring |
N |
|||
. (period) |
Matches any single character. |
in.e |
winter has come. |
Y |
mother of invention |
Y |
|||
life is in everything |
Y |
|||
eight nine ten |
N |
|||
increasing population |
N |
|||
s..ing |
picnic in spring |
Y |
||
skiing in winter |
Y |
|||
[string]#1, #2 |
Matches any character specified in the string enclosed in square brackets. |
[pr] |
spring has come. |
Y |
today is monday. |
N |
|||
[char-char]#1, #2 |
Matches any character in the range, in ascending order of the character codes. |
[a-i] |
spring has come. |
Y |
[^char-char]#1, #2 |
Matches any character not in the specified range, in ascending order of the character codes. |
[^a-i] |
spring has come. |
Y |
char* |
Matches strings of zero or more occurrences of the preceding character. |
ro*m |
terminal |
Y |
cd-rom |
Y |
|||
living room |
Y |
|||
h.*n |
This is a pen. |
Y |
||
That is an apple. |
Y |
|||
regex|regex#1, #2, #3 |
Matches either regular expression. |
[0-9]+|apple |
That is an apple. |
Y |
spring in 2003 |
Y |
|||
\special-char#1, #2, #3 |
Handles the special character as an ordinary character. |
o\.h |
<stdio.h> |
Y |
another man |
N |
|||
(regex)#1, #2, #3 |
Groups a regular expression. Used to explicitly indicate to the program that the specified characters are a regular expression. Used mainly with a vertical bar (|). (See G.4 Tips on using regular expressions.) |
i(n.e|ng) |
winter has come. |
Y |
interesting book |
Y |