python正则表达式中的转义字符,Python正则表达式转义字符

We have:

>>> str

'exit\r\ndrwxr-xr-x 2 root root 0 Jan 1 2000

\x1b[1;34mbin\x1b[0m\r\ndrwxr-xr-x 3 root root

0 Jan 1 2000 \x1b[1;34mlib\x1b[0m\r\ndrwxr-xr-x 10 root

root 0 Jan 1 1970 \x1b[1;34mlocal\x1b[0m\r\ndrwxr-xr-x

2 root root 0 Jan 1 2000 \x1b[1;34msbin\x1b[0m\r\ndrwxr-xr-x

5 root root 0 Jan 1 2000 \x1b[1;34mshare\x1b[0m\r\n# exit\r\n'

>>> print str

exit

drwxr-xr-x 2 root root 0 Jan 1 2000 bin

drwxr-xr-x 3 root root 0 Jan 1 2000 lib

drwxr-xr-x 10 root root 0 Jan 1 1970 local

drwxr-xr-x 2 root root 0 Jan 1 2000 sbin

drwxr-xr-x 5 root root 0 Jan 1 2000 share

# exit

I want to get rid of all the '\xblah[0m' nonsense using regexp. I've tried

re.sub(str, r'(\x.*m)', '')

But that hasn't done the trick. Any ideas?

解决方案

You have a few issues:

You're passing arguments to re.sub in the wrong order wrong. It should be:

re.sub(regexp_pattern, replacement, source_string)

The string doesn't contain "\x". That "\x1b" is the escape character, and it's a single character.

As interjay pointed out, you want ".*?" rather than ".*", because otherwise it will match everything from the first escape through the last "m".

The correct call to re.sub is:

print re.sub('\x1b.*?m', '', s)

Alternatively, you could use:

print re.sub('\x1b[^m]*m', '', s)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值