sed to python:替换Q. [英] sed to python: replace Q

查看:68
本文介绍了sed to python:替换Q.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我无法理解Python的string.replace()函数。

只是试图解析一个简单的IP地址,用方括号括起来,
来自Postfix日志的
。在sed中,这是直截了当的:


line =" date process text [ip] more text"


sed -e's / ^.* \ [|''-e's /]。* $ //''


但以下Python代码什么都不做:


line = line.replace(''^。* \ ['','''',1)

line = line.replace('']。* $'','''')


某处是否有一个体面的string.replace()描述?


Raymond

解决方案

//''


但以下Python代码什么都不做:


line = line.replace(''^。* \ ['','''',1)

line = line.replace('']。*

'','''')


某处是否有一个体面的string.replace()描述?


Raymond





2008/4/30 Raymond< no ******** **@sonic.net&g t;:


由于某种原因,我无法理解Python的string.replace()函数。



replace()不适用于正则表达式。


是否有正确的字符串描述。在某处替换()?



使用re.sub()。


>> import re
line =" date process text [ip] more text"
re.sub('']。*


For some reason I''m unable to grok Python''s string.replace() function.
Just trying to parse a simple IP address, wrapped in square brackets,
from Postfix logs. In sed this is straightforward given:

line = "date process text [ip] more text"

sed -e ''s/^.*\[//'' -e ''s/].*$//''

yet the following Python code does nothing:

line = line.replace(''^.*\['', '''', 1)
line = line.replace(''].*$'', '''')

Is there a decent description of string.replace() somewhere?

Raymond

解决方案

//''

yet the following Python code does nothing:

line = line.replace(''^.*\['', '''', 1)
line = line.replace(''].*


'', '''')

Is there a decent description of string.replace() somewhere?

Raymond


Hi,

2008/4/30 Raymond <no**********@sonic.net>:

For some reason I''m unable to grok Python''s string.replace() function.

replace() does not work with regular expressions.

Is there a decent description of string.replace() somewhere?

Use re.sub().

>>import re
line = "date process text [ip] more text"
re.sub(''].*


这篇关于sed to python:替换Q.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆