带尖括号的正则表达式行为 [英] Regex behaviour with angle brackets

查看:477
本文介绍了带尖括号的正则表达式行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请向我解释为什么以下表达式不输出任何内容:

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

echo "<firstname.lastname@domain.com>" | egrep "<lastname@domain.com>"

但以下内容确实存在:

echo "<firstname.lastname@domain.com>" | egrep "\<lastname@domain.com>"

第一个的行为与预期的一样,但第二个的行为不应输出.是"\<"在正则表达式中被忽略还是引起其他特殊行为?

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?

推荐答案

AS @hwnd说\<与单词的开头匹配. ie单词边界\b必须存在于起始单词字符之前(输入中\<之后的字符必须是单词字符)

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),

在您的示例中,

echo "<firstname.lastname@domain.com>" | egrep "<lastname@domain.com>"

在上面的示例中,egrep检查姓氏字符串之前的文字<字符.但是没有,所以它什么也不打印.

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

$ echo "<firstname.lastname@domain.com>" | egrep "\<lastname@domain.com>"
<firstname.**lastname@domain.com>**

但是在此示例中,在lastname字符串之前存在单词边界\b,因此它会打印匹配的字符.

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

更多示例:

$ echo "namelastname@domain.com" | egrep "\<e@domain.com"
$ echo "namelastname@domain.com" | egrep "\<lastname@domain.com"
$ echo "namelastname@domain.com" | egrep "\<com"
namelastname@domain.**com**
$ echo "<firstname.lastname@domain.com>" | egrep "\<@domain.com>"
$ echo "n-ame-lastname@domain.com" | egrep "\<ame-lastname@domain.com"
n-**ame-lastname@domain.com**

这篇关于带尖括号的正则表达式行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆