如何用Python一行程序替换Perl一行程序正则表达式?
我在一个不使用Perl的项目上工作,我希望保持一致性。这就是为什么我想知道是否可以轻松地用Python one liner替换这个方便的Perl one liner:如何用Python一行程序替换Perl一行程序正则表达式?,python,regex,command-line-interface,Python,Regex,Command Line Interface,我在一个不使用Perl的项目上工作,我希望保持一致性。这就是为什么我想知道是否可以轻松地用Python one liner替换这个方便的Perl one liner: perl-pe's/pattern/replacement/g'您可以使用-c命令行选项运行re.sub,但它不会像perl那样漂亮: python -c 'import re;print(re.sub(r"<pattern>", "<replacement>", &
perl-pe's/pattern/replacement/g'您可以使用-c
命令行选项运行re.sub
,但它不会像perl
那样漂亮:
python -c 'import re;print(re.sub(r"<pattern>", "<replacement>", "<string>"))'
例如:
% python -c 'import re;print(re.sub(r"foo", "bar", "foobar"))'
barbar
% python -c 'import re,sys;print(re.sub(r"foo", "bar", sys.stdin.read()))' <<< 'foobar'
barbar
ruby -pe 'gsub /[es]/, "X"' <<< 'expression'
%python-c'import-re;印刷品(再细分(r“foo”、“bar”、“foobar”))
芭芭拉
%python-c'导入re,sys;print(re.sub(r“foo”,“bar”,sys.stdin.read())”Perl一行程序的更现代的替代品是Ruby,有人说它是Perl 6。例如:
% python -c 'import re;print(re.sub(r"foo", "bar", "foobar"))'
barbar
% python -c 'import re,sys;print(re.sub(r"foo", "bar", sys.stdin.read()))' <<< 'foobar'
barbar
ruby -pe 'gsub /[es]/, "X"' <<< 'expression'
ruby-pe'gsub/[es]/,“X”'trued re.findall?sed
通常会取代它。不过,我希望能够使用Python正则表达式引擎。