Question
Making sense of the regex in grep command
When executing the following command in a bash shell:
grep '^\(.\)*\1$' exp.txt
I expect it to match any line that:
- begins and ends with the same latter.
but in reality it matches the following lines:
Ritchie at Bell
Laboratories in the late 1960s. Onee
- first of all my name is ...
TTTTTTTTTTTTTT
I understand why it selected TTTTTTTTTTTTTT
since it begins and ends with the same character, but I don't know why it selected the rest.
I know that adding a dot before the asterisk '^\(.\).*\1$'
will do what I desire (selecting lines that begin and end with the same character), but I still would like to know why '^\(.\)*\1$'
doesn't work as expected.
The full file (exp.txt) is:
The Unix operating system was pioneered
by
Ken
Thompson and Dennis
Ritchie at Bell
Laboratories in the late 1960s. Onee
of the primary
goals in the design of the Unix system was to create an environment that
promoted efficient program
development.
[my name]
.Hi man
HHi man
my mamamamamama my
mohammad
space spacesm
first char is same as last f
- first of all my name is ...
T
TT
TTTT
TTTTT
TTTTTT
TTTTTTT
TTTTTTTTTTTTTT