JavaScript Editor Ajax software     Free javascripts 



Main Page

colours (British English, plural noun)
color’s (U.S. English, possessive singular)
colour’s (British English, possessive singular)
colors’ (U.S. English, possessive plural)
colours’ (British English, possessive plural)
The following regular expression pattern, which includes three optional characters, can match all eight
of these word forms:
colou?r’?s?’?
If you tried to express this in a semiformal way, you might have the following problem definition:
Match the U.S. English and British English forms of
color
(
colour
), including the singular noun,
the plural noun, and the singular possessive and the plural possessive.
Try it out, and then I will explain why it works and what limitations it potentially has.
Matching Multiple Optional Characters
Use the sample file
Colors2.txt
to explore this example:
These colors are bright.
Some colors feel warm. Other colours feel cold.
A color’s temperature can be important in creating reaction to an image.
These colours’ temperatures are important in this discussion.
Red is a vivid colour.
To test the regular expression, follow these steps:
1.
Open OpenOffice.org Writer, and open the file
Colors2.txt
.
2.
Use the keyboard shortcut Ctrl+F to open the Find And Replace dialog box.
3.
Check the Regular Expressions check box and the Match Case check box.
4.
In the Search For text box, enter the regular expression pattern
colou?r’?s?’?
, and click the Find
All button. If all has gone well, you should see the matches shown in Figure A-16.
329
Appendix A: Simple Regular Expressions
bapp01.qxd:bapp01 10:47 329


JavaScript Editor Ajax software     Free javascripts