|
|
|
|
| 如何实现替换功能:"foo bar"; s/(foo)/$1 $1/;=>"foo foo bar" |
 如何实现替换功能:"foo bar"; s/(foo)/$1 $1/;=>"foo foo bar" - alula [ 2005-07-13 16:58 | 104 byte(s)]
 Re: 如何实现替换功能:"foo bar"; s/(foo)/$1 $1/;=>"foo foo bar" - passworld [ 2005-07-13 19:29 | 374 byte(s)]
 Re: 如何实现替换功能:"foo bar"; s/(foo)/$1 $1/;=>"foo foo - alula [ 2005-07-14 09:16 | 800 byte(s)]
 Re: 如何实现替换功能:"foo bar"; s/(foo)/$1 $1/;=>"foo foo - limodou [ 2005-07-14 10:17 | 20 byte(s)]
 Re: 如何实现替换功能:"foo bar"; s/(foo)/$1 $1/;=>"foo foo - alula [ 2005-07-14 10:40 | 602 byte(s)]
 Re: 如何实现替换功能:"foo bar"; s/(foo)/$1 $1/;=>"foo foo - limodou [ 2005-07-15 10:53 | 56 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
手册里 re 部分 sub() 应该讲得很清楚,可以用 1 或者 g<1>
>>> re.sub(r'(foo)', r'g<1> g<1>', 'foo bar')
'foo foo bar'
>>> re.sub(r'(foo)', r'1 1', 'foo bar')
'foo foo bar'
>>>
|
|
|
----
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
谢谢哦!
说真,还没有好好仔细的看这个手册,只是匆匆翻了一下<Learning Python> 和<Programming Python>,一时没看到。
<Programming Python>是提到的,现在看到了。:)
umber
Matches the contents of the group of the same number. Groups are numbered starting from 1. For example, (.+) 1 matches 'the the' or '55 55', but not 'the end' (note the space after the group). This special sequence can only be used to match one of the first 99 groups. If the first digit of number is 0, or number is 3 octal digits long, it will not be interpreted as a group match, but as the character with octal value number. Inside the "[" and "]" of a character class, all numeric escapes are treated as characters.
|
|
|
----
温故知新
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
好像是Python程序员捡到便宜了,Python超值大甩卖。:)
另
>>> h = { 'foo' : '$foo', 'bar' : '$bar' }
>>> txt = '<> foo <> bar <>'
如果要按照h的内容,把txt 替换成 '<> $foo <> $bar <>'。看了手册,大概可以这么写:
>>> re.sub('(w+)', lambda x: str(h.get(x.group(1))), txt)
'<> $foo <> $bar <>'
>>>
|
|
|
----
温故知新
|
|
[Original]
[Print]
[Top]
|
|
|