URN Logo
UNIX Resources » Linux » China Linux Forum » Python 编 程 » 8 » 如何实现替换功能:"foo bar"; s/(foo)/$1 $1/;=>"foo foo bar"
announcement 声明: 本页内容为中国Linux论坛的内容镜像,文章的版权以及其他所有的相关权利属于中国Linux论坛和相应文章的作者,如果转载,请注明文章来源及相关版权信息。
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.net
  业界新闻与评论
  自由软件杂谈
  IT 人生
  Linux软件快递
  翻译作坊
  Linux图书与评论
  GNU Emacs/XEmacs
  Linux 中文环境和中文化
  Linux桌面与办公软件
  Linux 多媒体与娱乐版
  自由之窗Mozilla
  笔记本电脑上的Linux
  Gentoo
  Debian 一族
  网络管理技术
  Linux 安装与入门
  WEB服务器和FTP服务器
  域名服务器和邮件服务器
  Linux防火墙和代理服务器应用
  文件及打印服务器
  技术培训与认证
  Linux内核技术
  Linux 嵌入技术
  Linux设备驱动程序
  Linux 集群技术
  LINUX平台数据库
  系统和网络安全
  CPU 与 编译器
  系统计算研究所专栏
  Linux下的GUI软件开发
  C/C++编程版
  PHP 技 术
  Java&jsp技术
  Shell编程技术
  Perl 编 程
  Python 编 程
  XML/Web Service 技术
  永远的Unix
  FreeBSD世界
   
如何实现替换功能:"foo bar"; s/(foo)/$1 $1/;=>"foo foo bar"
 
 
 
 
 
 
Subject: 如何实现替换功能:"foo bar"; s/(foo)/$1 $1/;=>"foo foo bar"
Author: alula    Posted: 2005-07-13 16:58    Length: 104 byte(s)
[Original] [Print] [Top]
在perl有$1这样的上下文,所以快捷。sed也有类似的功能。
那么Python 做这样的替换,怎么写比较好呢?
----
温故知新
[Original] [Print] [Top]
Subject: Re: 如何实现替换功能:"foo bar"; s/(foo)/$1 $1/;=>"foo foo bar"
Author: passworld    Posted: 2005-07-13 19:29    Length: 374 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]
Subject: Re: 如何实现替换功能:"foo bar"; s/(foo)/$1 $1/;=>"foo foo
Author: alula    Posted: 2005-07-14 09:16    Length: 800 byte(s)
[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]
Subject: Re: 如何实现替换功能:"foo bar"; s/(foo)/$1 $1/;=>"foo foo
Author: limodou    Posted: 2005-07-14 10:17    Length: 20 byte(s)
[Original] [Print] [Top]
python绝对是超值的。
----
[Original] [Print] [Top]
Subject: Re: 如何实现替换功能:"foo bar"; s/(foo)/$1 $1/;=>"foo foo
Author: alula    Posted: 2005-07-14 10:40    Length: 602 byte(s)
[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]
Subject: Re: 如何实现替换功能:"foo bar"; s/(foo)/$1 $1/;=>"foo foo
Author: limodou    Posted: 2005-07-15 10:53    Length: 56 byte(s)
[Original] [Print] [Top]
对。re.sub可以调用一个回调函数,做一些特殊处理非常方便。
----
[Original] [Print] [Top]
« Previous thread
弱弱地问一下,怎样运行helloworld.py?
Python 编 程
8
Next thread »
项目合作,北京
     

Copyright © 2007 UNIX Resources Network, All Rights Reserved.      About URN | Privacy & Legal | Help | Contact us
备案序号: 京ICP备05006143    webmaster: webmaster@unixresources.net
This page created on 2008-07-17 04:11:09, cost 0.053683996200562 ms.