OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

+ - Match An Element One Or More Times

The + metacharacter works much like the *, except it requires at least one instance of the preceding element to cause a match. Here is a regular expression that will only match lines consisting of groups of one or more alphabetic characters separated by single spa- ces:

^([[:alpha:]]+ ?)+$


[me@linuxbox ~]$ echo "This that" | grep -E '^([[:alpha:]]+ ?)+$'

This that

[me@linuxbox ~]$ echo "a b c" | grep -E '^([[:alpha:]]+ ?)+$'

a b c

[me@linuxbox ~]$ echo "a b 9" | grep -E '^([[:alpha:]]+ ?)+$'

[me@linuxbox ~]$ echo "abc d" | grep -E '^([[:alpha:]]+ ?)+$'

[me@linuxbox ~]$

[me@linuxbox ~]$ echo "This that" | grep -E '^([[:alpha:]]+ ?)+$'

This that

[me@linuxbox ~]$ echo "a b c" | grep -E '^([[:alpha:]]+ ?)+$'

a b c

[me@linuxbox ~]$ echo "a b 9" | grep -E '^([[:alpha:]]+ ?)+$'

[me@linuxbox ~]$ echo "abc d" | grep -E '^([[:alpha:]]+ ?)+$'

[me@linuxbox ~]$


We see that this expression does not match the line “a b 9”, because it contains a non-al- phabetic character; nor does it match “abc d”, because more than one space character separates the characters “c” and “d”.


Top OS Cloud Computing at OnWorks: