我想要
ereg ($rat, $dog, $cat)
成为
preg_match ('#'.$rat.'#', $dog, $cat)
为实现这一目标,我做了
echo 'ereg ($rat, $dog, $cat)' | sed "s/ereg\(.*\)(\(.*\),/preg_match\1('#'.\2.'#',/g"
但是,这个正则表达式改变了
ereg ($rat, $dog, $cat)
到
preg_match ('#'.$rat, $dog.'#', $cat)
而不是
preg_match ('#'.$rat.'#', $dog, $cat)
有人可以帮我构建一个改变的正则表达式
ereg ($rat, $dog, $cat)
到
preg_match ('#'.$rat.'#', $dog, $cat)
答案 0 :(得分:2)
只需排除','...
echo 'ereg ($rat, $dog, $cat)' | sed "s/ereg\(.*\)(\([^,]*\),/preg_match\1('#'.\2.'#',/g"
答案 1 :(得分:2)
注意:如果您在同一行上有多个ereg ($rat, $dog, $cat)
个实例,而其他答案没有,则此功能也适用:
$ echo 'ereg ($rat, $dog, $cat)' | sed "s/ereg\([^(]*\)(\([^,]*\),\([^)]*)\)/preg_match\1('#'.\2.'#',\3/g"
preg_match ('#'.$rat.'#', $dog, $cat)
$ echo 'ereg ($rat, $dog, $cat) ereg ($rat, $dog, $cat)' | sed "s/ereg\([^(]*\)(\([^,]*\),\([^)]*)\)/preg_match\1('#'.\2.'#',\3/g"
preg_match ('#'.$rat.'#', $dog, $cat) preg_match ('#'.$rat.'#', $dog, $cat)
答案 2 :(得分:1)
~ ross$ echo 'ereg ($rat, $dog, $cat)' | sed -e "s/.*(\([^,]*\),\([^,]*\),\([^)]*\))/preg_match ('#'.\1.'#', \2, \3)/"
preg_match ('#'.$rat.'#', $dog, $cat)
答案 3 :(得分:1)
怎么样:
$ echo 'ereg ($rat, $dog, $cat)' | sed "s/ereg *(\([^,]*\), \([^,]*\)\([^)]*\))/preg_match('#'.\1.'#', \2\3)/g"
preg_match('#'.$rat.'#', $dog, $cat)
答案 4 :(得分:0)
我更喜欢Perl正则表达式(更容易编写):
echo 'ereg ($rat, $dog, $cat)' | perl -pe 's/ereg\s*\(\s*(\$[^,]+),(.+)\)/preg_match("#" . $1 . "#", $2)/g'
或更好(对于PHP):
echo 'ereg ($rat, $dog, $cat)' | perl -pe 's/ereg\s*\(\s*(\$[^,]+),(.+)\)/preg_match("#{$1}#", $2)/g'
在 sed 中,它将是:
echo 'ereg ($rat, $dog, $cat)' | sed -e 's/ereg\s*(\s*\(\$[^,]\+\),\(.\+\))/preg_match("#" . \1 . "#", \2)/'
太多的反斜杠!!!