Shell 编程 正则表达式
本篇主要写一些 概述正则表达式分为基础正则表达式(Regular Expression)与扩展正则表达式(Extended Regular Expression)。 它不是一个工具程序,而是一个字符串处理的标准依据,是使用单个字符串搜索、匹配一系列符合某个语法规则的字符串。 它由普通字符(a~z)以及特殊字符(元字符)组成。 linux 文本处理工具
基础正则表达式
he was short and fat. He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online. the tongue is boneless but it breaks bones.12! google is the best tools for search keyword. The year ahead will test our political establishment to the limit. PI=3.141592653589793238462643383249901429 a wood cross! Actions speak louder than words #woood # #woooooood # AxyzxyzxyzxyzC I bet this place is really spooky late at night! Misfortunes never come alone/single. I shouldn't have lett so tast. 特定字符
[[email?protected] ~]# grep -n 'the' test.txt 4:the tongue is boneless but it breaks bones.12! 5:google is the best tools for search keyword. 6:The year ahead will test our political establishment to the limit. [[email?protected] ~]# grep -in 'the' test.txt 3:The home of Football on BBC Sport online. 4:the tongue is boneless but it breaks bones.12! 5:google is the best tools for search keyword. 6:The year ahead will test our political establishment to the limit. [[email?protected] ~]# grep -vn 'the' test.txt 1:he was short and fat. 2:He was wearing a blue polo shirt with black pants. 3:The home of Football on BBC Sport online. 7:PI=3.141592653589793238462643383249901429 8:a wood cross! 9:Actions speak louder than words 10: 11: 12:#woood # 13:#woooooood # 14:AxyzxyzxyzxyzC 15:I bet this place is really spooky late at night! 16:Misfortunes never come alone/single. 17:I shouldn't have lett so tast. 集合“[]”
[[email?protected] ~]# grep -n 'sh[io]rt' test.txt 1:he was short and fat. 2:He was wearing a blue polo shirt with black pants.
[[email?protected] ~]# grep -n 'oo' test.txt 3:The home of Football on BBC Sport online. 5:google is the best tools for search keyword. 8:a wood cross! 12:#woood # 13:#woooooood # 15:I bet this place is really spooky late at night!
[[email?protected] ~]# grep -n '[^w]oo' test.txt 3:The home of Football on BBC Sport online. 5:google is the best tools for search keyword. 12:#woood # 13:#woooooood # 15:I bet this place is really spooky late at night!
[[email?protected] ~]# grep -n '[^a-z]oo' test.txt 3:The home of Football on BBC Sport online.
[[email?protected] ~]# grep -n '[0-9]' test.txt 4:the tongue is boneless but it breaks bones.12! 7:PI=3.141592653589793238462643383249901429 行首“^”
[[email?protected] ~]# grep -n '^the' test.txt 4:the tongue is boneless but it breaks bones.12!
[[email?protected] ~]# grep -n '^[a-z]' test.txt 1:he was short and fat. 4:the tongue is boneless but it breaks bones.12! 5:google is the best tools for search keyword. 8:a wood cross!
[[email?protected] ~]# grep -n '^[A-Z]' test.txt 2:He was wearing a blue polo shirt with black pants. 3:The home of Football on BBC Sport online. 6:The year ahead will test our political establishment to the limit. 7:PI=3.141592653589793238462643383249901429 9:Actions speak louder than words 14:AxyzxyzxyzxyzC 15:I bet this place is really spooky late at night! 16:Misfortunes never come alone/single. 17:I shouldn't have lett so tast.
[[email?protected] ~]# grep -n '^[^a-zA-Z]' test.txt 12:#woood # 13:#woooooood # 行尾“$”
[[email?protected] ~]# grep -n '.$' test.txt 1:he was short and fat. 2:He was wearing a blue polo shirt with black pants. 3:The home of Football on BBC Sport online. 5:google is the best tools for search keyword. 6:The year ahead will test our political establishment to the limit. 16:Misfortunes never come alone/single. 17:I shouldn't have lett so tast.
[[email?protected] ~]# grep -n '^$' test.txt 10: 11: 任意一个字符“.”
[[email?protected] ~]# grep -n 'w..d' test.txt 5:google is the best tools for search keyword. 8:a wood cross! 9:Actions speak louder than words 重复字符“*”
[[email?protected] ~]# grep -n 'ooo*' test.txt 3:The home of Football on BBC Sport online. 5:google is the best tools for search keyword. 8:a wood cross! 12:#woood # 13:#woooooood # 15:I bet this place is really spooky late at night!
[[email?protected] ~]# grep -n 'woo*d' test.txt 8:a wood cross! 12:#woood # 13:#woooooood #
[[email?protected] ~]# grep -n 'w.*d' test.txt 1:he was short and fat. 5:google is the best tools for search keyword. 8:a wood cross! 9:Actions speak louder than words 12:#woood # 13:#woooooood #
[[email?protected] ~]# grep -n '[0-9][0-9]*' test.txt 4:the tongue is boneless but it breaks bones.12! 7:PI=3.141592653589793238462643383249901429 连续字符范围“{}”
[[email?protected] ~]# grep -n 'o{2}' test.txt 3:The home of Football on BBC Sport online. 5:google is the best tools for search keyword. 8:a wood cross! 12:#woood # 13:#woooooood # 15:I bet this place is really spooky late at night!
[[email?protected] ~]# grep -n 'wo{2,5}d' test.txt 8:a wood cross! 12:#woood #
[[email?protected] ~]# grep -n 'wo{2,}d' test.txt 8:a wood cross! 12:#woood # 13:#woooooood # 扩展正则表达式
[[email?protected] ~]# egrep -n 'wo+d' test.txt 8:a wood cross! 12:#woood # 13:#woooooood #
[[email?protected] ~]# egrep -n 'bes?t' test.txt 5:google is the best tools for search keyword. 15:I bet this place is really spooky late at night!
[[email?protected] ~]# egrep -n 'of|is|on' test.txt 3:The home of Football on BBC Sport online. 4:the tongue is boneless but it breaks bones.12! 5:google is the best tools for search keyword. 6:The year ahead will test our political establishment to the limit. 9:Actions speak louder than words 15:I bet this place is really spooky late at night! 16:Misfortunes never come alone/single.
[[email?protected] ~]# egrep -n 't(a|e)st' test.txt 6:The year ahead will test our political establishment to the limit. 17:I shouldn't have lett so tast.
[[email?protected] ~]# egrep -n 'A(xyz)+C' test.txt 14:AxyzxyzxyzxyzC (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |