Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用Python一行程序替换Perl一行程序正则表达式?_Python_Regex_Command Line Interface - Fatal编程技术网

如何用Python一行程序替换Perl一行程序正则表达式?

如何用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的项目上工作,我希望保持一致性。这就是为什么我想知道是否可以轻松地用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>", "<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正则表达式引擎。