php shell vim 正则表达式对比

在各个环境下使用正则表达式会有细微的区别,几乎不好察觉,很容易得到错误的结果:因此对比下shell、php、vim下各个grep的方法

PS shell下使用的是egrep,珍爱生命,远离GREP.有很多的通用特性不能使用,会很郁闷的。

 

--- cite from 

http://www.chinaunix.net/jh/24/446683.html

* egrep: 
为 grep 的扩充版本, 改良了许多传统 grep 不能或不便的操作. 比方说: 
- grep 之下不支持 ? 与 + 这两种 modifier, 但 egrep 则可. 
- grep 不支持 a|b 或 (abc|xyz) 这类"或一"比对, 但 egrep 则可. 
- grep 在处理 {n,m} 时, 需用 \{ 与 \} 处理, 但 egrep 则不需. 
诸如此类的... 我个人会建议能用 egrep 就不用 grep 啦... ^_^ 

 

--- end of cite

 

1花括号{ } ,用于存放符合匹配的字符集

php:

$string = "abcdefghijklm";
if (preg_match('/[a-z]{13}/',$string)) {
    
echo "13 match\n";
}

if (!preg_match('/[a-z]{14}/',$string)) {
    
echo "14 not match\n";
}

 

if (preg_match('/[a-z]{1,15}/',$string)) {
        echo "1 - 15 match\n";
}

 

 

shell: grep 需要加\, egrep不需要

/test/regex$ egrep '[a-m]{13}' test.php    
$string = "abcdefghijklm";
/test/regex$ grep '[a-m]\{13\}' test.php  
$string = "abcdefghijklm";
/test/regex$ grep '[a-m]{13}' test.php
(no result)


vim:  注意 是单边斜杠

/[a-z]\{13}

 

2 或操作

php:
$pattern = '/(aeiou|abegh)/';
if (!preg_match($pattern,$string)) {
        
echo $pattern."not match\n";
}

$pattern = '/(abcde|abegh)/';
if (preg_match($pattern,$string)) {
        
echo $pattern." match\n";
}

result:

/(aeiou|abegh)/not match
/(abcde|abegh)/ match
  

 

shell: egrep支持 "|"   grep 不支持

~/test/regex$ egrep '(abc|echo)' test.php    
$string = "abcdefghijklm";
        echo 
"13 match\n";
        echo 
"14 not match\n";
        echo 
"1 - 15 match\n";


vim: 使用 \| 进行或操作

/abcde\|aeiou

 

 

 3 分组

 php:

$pattern = '/(duxiaola)@\1/';
$string = 'duxiaola@duxiaola';
if (preg_match($pattern,$string)) {
        
echo "pattern: $pattern\n string: $string\n match\n";
}

 pattern: /(duxiaola)@\1/

 string: duxiaola@duxiaola
 match

 

egrep:

 

~/fwz/test/regex$ echo duxiaola@duxiaola | egrep '(duxiaola)@\1'
duxiaola
@duxiaola

~
/fwz/test/regex$ echo duxiaola@duxiaola | egrep '.(uxiaola)@.\1'
duxiaola
@duxiaola

 

 vim:   也是使用\( \)来进行

/\(duxiaola\)@\1

 

 

最常用的是这几个,以后继续补充 。

 

4 单词边界

注意:这是非[a-zA-Z0-9]的意思

 

php使用\b标识单词边界

$pattern = '/\bab/';
$strings = array(
                'aab',  
                'ab',   
                '
@ab',  
                '3ab',  
);
for ( $i = 0 ; $i < count($strings) ; $i++){
    
if (preg_match($pattern$strings[$i])) {
        echo 
"pattern: $pattern\n string: $strings[$i]\n match\n";
    }
}

 result:

 

pattern: /\bab/
 string: ab
 match
pattern: /\bab/
 string: @ab
 match
 

 

grep / egrep / vim 都用 \< \> 来标识单词边界

/\<ab

 

 

小结:

php和egrep在这几个操作上基本是一致的

vim需要加\来实现这些操作……

 

 

这里还有:http://www.cnblogs.com/HectorInsanE/archive/2010/10/21/1857804.html

 

 

posted @ 2011-11-10 14:23  Hector  阅读(413)  评论(0编辑  收藏  举报