Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex behaviour with angle brackets

Tags:

regex

grep

Please explain to me why the following expression doesn't output anything:

echo "<[email protected]>" | egrep "<[email protected]>"

but the following does:

echo "<[email protected]>" | egrep "\<[email protected]>"

The behaviour of the first is as expected but the second should not output. Is the "\<" being ignored within the regex or causing some other special behaviour?

like image 456
Shane Carr Avatar asked Mar 19 '23 02:03

Shane Carr


1 Answers

AS @hwnd said \< matches the begining of the word. ie a word boundary \b must exists before the starting word character(character after \< in the input must be a word character),

In your example,

echo "<[email protected]>" | egrep "<[email protected]>"

In the above example, egrep checks for a literal < character present before the lastname string. But there isn't, so it prints nothing.

$ echo "<[email protected]>" | egrep "\<[email protected]>"
<firstname.**[email protected]>**

But in this example, a word boundary \b exists before lastname string so it prints the matched characters.

Some more examples:

$ echo "[email protected]" | egrep "\<[email protected]"
$ echo "[email protected]" | egrep "\<[email protected]"
$ echo "[email protected]" | egrep "\<com"
namelastname@domain.**com**
$ echo "<[email protected]>" | egrep "\<@domain.com>"
$ echo "[email protected]" | egrep "\<[email protected]"
n-**[email protected]**
like image 178
Avinash Raj Avatar answered Mar 29 '23 17:03

Avinash Raj