If I grep a document that contains the following:
ThisExampleString
…for the expression This*String
or *String
, nothing is returned. However, This*
returns the above line as expected.
Whether the expression is enclosed in quotes makes no difference.
I thought the asterisk indicated any number of unknown characters? Why does it only work if it's at the start of the expression? If this is intended behavior, what do I use instead of the expressions This*String
and *String
?
Best Answer
An asterisk in regular expressions means "match the preceding element 0 or more times".
In your particular case with
grep 'This*String' file.txt
, you are trying to say, "hey, grep, match me the wordThi
, followed by lowercases
zero or more times, followed by the wordString
". The lowercases
is nowhere to be found inExample
, hence grep ignoresThisExampleString
.In the case of
grep '*String' file.txt
, you are saying "grep, match me the empty string--literally nothing--preceding the wordString
". Of course, that's not howThisExampleString
is supposed to be read. (There are other possible meanings--you can try this with and without the-E
flag--but none of the meanings are anything like what you really want here.)Knowing that
.
means "any single character", we could do this:grep 'This.*String' file.txt
. Now the grep command will read it correctly:This
followed by any character (think of it as selection of ASCII characters) repeated any number of times, followed byString
.