Take the pattern
[UGLER]*
Can the string UUG match against it? I mean to say, is repetition allowed?
Take the pattern
[UGLER]*
Can the string UUG match against it? I mean to say, is repetition allowed?
In principle yes, but that may depend on the regex flavor you are using. At the very least, BRE, ERE and PCRE will all match that string. The expression [UGLER]*
means match 0 or more consecutive characters from the set of U,G,L,E or R.
You can test this for different regex types easily enough:
BRE
$ echo UUG | grep '[UGLER]*'
UUG
ERE
$ echo UUG | grep -E '[UGLER]*'
UUG
PCRE
$ echo UUG | grep -P '[UGLER]*'
UUG
Of course, since you are looking for zero or more, it will also match things you might not be expecting:
$echo "foobar" | grep '[UGLER]*'
foobar
If the regex flavor you are using supports it, use the +
instead of *
. For example, with PCRE:
$echo -e "UUG\nfoobar" | grep -P '[UGLER]*'
UUG
foobar
$echo -e "UUG\nfoobar" | grep -P '[UGLER]+'
UUG
Assuming that your pattern is a fileglob pattern and not a regexp, then yes it will match a filename called 'UUG'. The pattern will match any file starting with U, G, L, E, or R.
you can test this yourself with:
touch UUG
ls -l [UGLER]*
If the pattern is a regexp, then it will match ANY string, because you are matching against zero-or-more instances of [UGLER]. If you want to match 1-or-more rather than zero-or-more, then use +
instead of *
+
- but some require you to write it as\+
– cas Sep 18 '13 at 03:19\+
, that's a GNUism. See there for more details. – Stéphane Chazelas Sep 18 '13 at 06:08[UGLER]+
, use[UGLER][UGLER]*
(ie, one occurence, followed by 0, 1 or many occurence) – Olivier Dulac Sep 18 '13 at 08:46+
was non-standard. I usually install GNU tools under /usr/local/ within hours or minutes of using non-GNU systems like *bsd or solaris, just to keep myself sane. – cas Sep 18 '13 at 12:03grep -E '[UGLER]+'
andgrep '[UGLER]\{1,\}'
are standard, it's justgrep '[UGLER]\+'
that isn't. – Stéphane Chazelas Sep 18 '13 at 12:39