python 使用通配符搜索替换
有点困惑..但是尝试使用通配符进行搜索/替换,
如果我有类似的事情:
<blah.... ssf ff>
<bl.... ssf dfggg ff>
<b.... ssf ghhjj fhf>
并且我想用比如说替换上述所有字符串,
<hh >t
关于如何实现这一点有任何想法/评论吗?
感谢
更新(感谢您的评论!)
我错过了一些东西......
我最初的示例文本是:
Soo Choi</span>LONGEDITBOX">Apryl Berney
Soo Choi</span>LONGEDITBOX">Joel Franks
Joel Franks</span>GEDITBOX">Alexander Yamato
我试图得到
Soo Choi foo Apryl Berney
Soo Choi foo Joel Franks
Joel Franks foo Alexander Yamato
我已经尝试过的推导,
name=re.sub("</s[^>]*\">"," foo ",name)
但我错过了一些东西......
想法......谢谢
somewhat confused.. but trying to do a search/repace using wildcards
if i have something like:
<blah.... ssf ff>
<bl.... ssf dfggg ff>
<b.... ssf ghhjj fhf>
and i want to replace all of the above strings with say,
<hh >t
any thoughts/comments on how this can be accomplished?
thanks
update (thanks for the comments!)
i'm missing something...
my initial sample text are:
Soo Choi</span>LONGEDITBOX">Apryl Berney
Soo Choi</span>LONGEDITBOX">Joel Franks
Joel Franks</span>GEDITBOX">Alexander Yamato
and i'm trying to get
Soo Choi foo Apryl Berney
Soo Choi foo Joel Franks
Joel Franks foo Alexander Yamato
i've tried derivations of
name=re.sub("</s[^>]*\">"," foo ",name)
but i'm missing something...
thoughts... thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样,用正则表达式怎么样
How about like this, with regex
请参阅相当有用的 Python 正则表达式 手册此处,或者如需更多实践方法,请访问 正则表达式 HOWTO 部分 5.2 搜索和替换。
See the rather usable Python Regular Expression manual here, or for a more hands-on approach a Regular Expression HOWTO section 5.2 Search and Replace.
不必使用正则
表达式输出
don't have to use regex
output
听起来像是“re”模块的一项工作,尽管您可以只使用一个 re.sub() 行,但这里有一个小示例函数。
使用“re”模块,一个简单的 re.sub 应该可以解决问题:
我建议查看“re”模块的文档,它有很好的文档记录,可以帮助您实现更准确的文本操作/替换。
Sounds like a job for the "re" module, here's a little sample function for you although you could just use the one re.sub() line.
Use the "re" module, a simple re.sub should do the trick:
I would suggest taking a look at the docs for the "re" module, it is well documented and might help you achieve more accurate text manipulation/replacement.