gawk '/^[A-Z][a-z]{3}/{print}‘怎么匹配不到Sjjj
发表于 : 2008-12-04 13:39
如题,gawk '/^[A-Z][a-z]{3}/{print}‘怎么匹配不到Sjjj
grep可以匹配到,怎么gawk匹配不到,gawk里面的正则表达式是可以用x{m}的形式的啊
注:用\{3\}也匹配不到
grep可以匹配到,怎么gawk匹配不到,gawk里面的正则表达式是可以用x{m}的形式的啊
注:用\{3\}也匹配不到
代码: 全选
r{n}
r{n,}
r{n,m} One or two numbers inside braces denote an interval expression. If there is one number in the braces, the preceding regular expression r is repeated n times. If there are two numbers separated by a comma, r is repeated n to m times. If there is one number followed by a comma, then r is repeated at least n times. Interval expressions are only available if either --posix or --re-interval is specified on the command line.
goodluck1982 写了:这里涉及到个问题,如果是第一次遇到都会很迷惑:
1. 在非英文的locale下,默认的英文字母排序方式可能不在是ASCII顺序,即ABCD...XYZabcd...xyz
例如,可能是AaBbCc...Zz或 aAbBcC...zZ,对于中文环境,似乎是后者。那么显而易见[A-Z]也匹配小写字母而[a-z]也匹配大写字母。所以最好用[[:upper:]]和[[:lower:]]来代表大写和小写字母,如果非要用[A-Z]来匹配大写字母的话,可以用设定locale,比如
LANG=C gawk '/A-Z/ {print}' ....
2. 区间操作 {n}, {n,}, {n,m} 等只有在使用 --posix 和 --re-interval 选项时才有效
关于这点,gawk的manpage里提到了代码: 全选
r{n} r{n,} r{n,m} One or two numbers inside braces denote an interval expression. If there is one number in the braces, the preceding regular expression r is repeated n times. If there are two numbers separated by a comma, r is repeated n to m times. If there is one number followed by a comma, then r is repeated at least n times. Interval expressions are only available if either --posix or --re-interval is specified on the command line.
高手不敢当princelai 写了:2楼是regex的高手阿
代码: 全选
[12-06 18:24> ~]# cat /boot/grub/menu.lst |grep -o '[A-Za-z]\{7\}'
generat
anacond
changes
partiti
relativ
vmlinuz
version
version
default
timeout
splashi
hiddenm
vmlinuz
vmlinuz
Windows
rootnov
chainlo
[12-06 18:24> ~]# gawk '/[A-Za-z]\{7\}/ {print}' /boot/grub/menu.lst
[12-06 18:24> ~]# gawk '/[A-Za-z]{7}/ {print}' /boot/grub/menu.lst
[12-06 18:24> ~]# gawk --posix '/[A-Za-z]{7}/ {print}' /boot/grub/menu.lst
# grub.conf generated by anaconda
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# kernel /vmlinuz-version ro root=/dev/sda12
# initrd /initrd-version.img
default=0
timeout=5
splashimage=(hd0,6)/grub/splash.xpm.gz
hiddenmenu
kernel /vmlinuz-2.6.25.14-108.fc9.i686 ro root=UUID=718df761-8b0a-45ca-a752-73bf8e0d099e rhgb quiet
kernel /vmlinuz-2.6.25-14.fc9.i686 ro root=UUID=718df761-8b0a-45ca-a752-73bf8e0d099e rhgb quiet
title Windows
rootnoverify (hd0,0)
chainloader +1
[12-06 18:24> ~]#
首先看你要匹配什么了,如LZ所说,匹配 Sjjj 的话eexpress 写了:[A-Z][a-z]{3} 不是这样写的。这是2个因子了啊。
[A-Za-z]\{7\} 这才是正则啊。