E.4 Examples of regular expressions
The following table shows examples of regular expressions.
Specification method |
Function |
Character string specified as the regular expression |
Pattern that matches or does not match the regular expression |
Result |
---|---|---|---|---|
character-string |
Matches a line that contains the specified character string. |
spring |
spring has come. |
Y |
winter-summer-autumn-spring |
Y |
|||
-----spring----- |
Y |
|||
^character-string |
Matches a line that begins with the specified character string. |
^spring |
spring has come. |
Y |
winter-summer-autumn-spring |
N |
|||
-----spring----- |
N |
|||
character-string$ |
Matches a line that ends with the specified character string. |
spring$ |
spring has come. |
N |
winter-summer-autumn-spring |
Y |
|||
-----spring----- |
N |
|||
^character-string$ |
Matches a line that contains only the specified character string. |
^spring$ |
spring has come. |
N |
winter-summer-autumn-spring |
N |
|||
spring |
Y |
|||
spring |
N |
|||
^$ |
Matches a null line. |
^$ |
Y |
|
spring |
N |
|||
. (period) |
Matches any one 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 |
|||
character* |
Matches a character string in which the character immediately preceding * is included any number of times or no times. |
ro*m |
terminal |
Y |
cd-rom |
Y |
|||
living room |
Y |
|||
h.*n |
This is a pen. |
Y |
||
That is an apple. |
Y |
|||
regular-expression|regular-expression |
Matches either the left-hand regular expression or the right-hand regular expression. |
[0-9]+|apple |
That is an apple. |
Y |
spring in 2009 |
Y |
|||
(regular-expressions) |
Groups regular expressions. This method is used mainly when the | is used. |
i(n.e|ng) |
winter has come. |
Y |
interesting book |
Y |
|||
[character-string] |
Matches any of the characters in the character string specified in [ ]. |
[pr] |
spring has come. |
Y |
today is monday. |
N |
|||
[character-character] |
Matches any of the characters within the specified range of character codes (when sorted in ascending order). |
[a-i] |
spring has come. |
Y |
[^character-character] |
Matches any characters not within the specified range of character codes (when sorted in ascending order). |
[^a-i] |
spring has come. |
Y |
\special-character |
Treats the specified special character as a normal character. |
o\.h |
<stdio.h> |
Y |
another man |
N |