OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

* - Match An Element Zero Or More Times

Like the ? metacharacter, the * is used to denote an optional item; however, unlike the ?, the item may occur any number of times, not just once. Let’s say we wanted to see if a string was a sentence; that is, it starts with an uppercase letter, then contains any number of upper and lowercase letters and spaces, and ends with a period. To match this (very crude) definition of a sentence, we could use a regular expression like this:

[[:upper:]][[:upper:][:lower:] ]*\.

The expression consists of three items: a bracket expression containing the [:upper:] character class, a bracket expression containing both the [:upper:] and [:lower:] character classes and a space, and a period escaped with a backslash. The second element is trailed with an * metacharacter, so that after the leading uppercase letter in our sen- tence, any number of upper and lowercase letters and spaces may follow it and still match:



[me@linuxbox ~]$ echo "This works." | grep -E '[[:upper:]][[:upper:][

:lower:] ]*\.'

This works.

[me@linuxbox ~]$ echo "This Works." | grep -E '[[:upper:]][[:upper:][

:lower:] ]*\.'

This Works.

[me@linuxbox ~]$ echo "this does not" | grep -E '[[:upper:]][[:upper:

][:lower:] ]*\.'

[me@linuxbox ~]$

[me@linuxbox ~]$ echo "This works." | grep -E '[[:upper:]][[:upper:][

:lower:] ]*\.'

This works.

[me@linuxbox ~]$ echo "This Works." | grep -E '[[:upper:]][[:upper:][

:lower:] ]*\.'

This Works.

[me@linuxbox ~]$ echo "this does not" | grep -E '[[:upper:]][[:upper:

][:lower:] ]*\.'

[me@linuxbox ~]$


The expression matches the first two tests, but not the third, since it lacks the required leading uppercase character and trailing period.


Top OS Cloud Computing at OnWorks: