Please enable JavaScript.
Coggle requires JavaScript to display documents.
20.正则表达式 (BRE 基本正则表达式 ^ $ . [] * ERE 扩展正则表达式() {} ? + | egrep 或者…
20.正则表达式
BRE 基本正则表达式 ^ $ . [] *
ERE 扩展正则表达式() {} ? + |
egrep 或者 GNU版本的grep -E
alternation
echo "BBB" | grep -E 'AAA|BBB'
grep -Eh '^(bz|gz|zip)' dirlist*.txt
限定符
? 匹配0或1次
* 匹配0次或n次
+ 匹配1次或n次
{} 匹配特定个数的元素
{n} 确切n次
{n,m} 至少n次不多于m次
{n,} 至少n次
{,m} 不多于m次
echo "(555) 123-4567" | grep -E '^(?[0-9]{3})? [0-9]{3}-[0-9]{4}$'
BRE \转义 from normal to meta
ERE \转义 from meta to normal
grep
(global regular expression print)
grep [options] regex [file...]
选项:
-i 忽略大小写
-v --invert-match
-c 打印匹配的数量
-l --files-with-matches 打印包含匹配的文件名 而不是文本行
-L 类似于-l 但相反 --files-without-match
-n 打印行号
-h 应用于多文件搜索 不输出文件名 --no-filename
原义字符
元字符
元字符
. 字符: 匹配任何一个字符
anchor(锚点):
^ 开头
$ 结尾
grep -h '^zip' dirlist*.txt
^$ 会匹配空行
/usr/share/dict 字典
grep -i '^..j.r$' /usr/share/dict/words
中括号表达式 匹配字符集合中的某个字符
grep -h '[bg]zip' dirlist-*.txt
元字符放入中括号中会失去特殊含义(除了^和-)
^
在第一个位置
表示否定(但仍然要求占有一个字符)
grep -h '[^bg]zip' dirlist-*.txt
传统的字符区域
grep -h '^[A-Z]' dirlist-*.txt
grep -h '^[A-Za-z0-9]' dirlist*.txt
使-在第一个位置 取消其字符区域的含义
grep -h '[-AZ]' dirlist-*.txt
POSIX字符集
echo $LANG
locale 查看Locale设置
find . -regex '.*[^-_./0-9a-zA-Z].*'
find 会要求 路径名 精确匹配正则表达式
locate --regexp BRE
--regex ERE
在less或vim中查找文本