python 使用通配符搜索替换

发布于 2024-08-17 22:54:01 字数 786 浏览 4 评论 0原文

有点困惑..但是尝试使用通配符进行搜索/替换,

如果我有类似的事情:

 <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

能怎样 2024-08-24 22:54:01

像这样,用正则表达式怎么样

import re

YOURTEXT=re.sub("<b[^>]*>","<hh >t",YOURTEXT)

How about like this, with regex

import re

YOURTEXT=re.sub("<b[^>]*>","<hh >t",YOURTEXT)
赤濁 2024-08-24 22:54:01

请参阅相当有用的 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.

旧夏天 2024-08-24 22:54:01

不必使用正则

for line in open("file"):
    if "<" in line and ">" in line:
        s=line.rstrip().split(">")
        for n,i in enumerate(s):
            if "<" in i:
                ind=i.find("<")
                s[n]=i[:ind] +"<hh "
        print '>t'.join(s)

表达式输出

$ cat file
blah  <blah.... ssf  ff> blah
blah <bl.... ssf     dfggg   ff>  blah <bl.... ssf     dfggg   ff>
blah <b.... ssf      ghhjj fhf>

$ ./python.py
blah  <hh >t blah
blah <hh >t  blah <hh >t
blah <hh >t

don't have to use regex

for line in open("file"):
    if "<" in line and ">" in line:
        s=line.rstrip().split(">")
        for n,i in enumerate(s):
            if "<" in i:
                ind=i.find("<")
                s[n]=i[:ind] +"<hh "
        print '>t'.join(s)

output

$ cat file
blah  <blah.... ssf  ff> blah
blah <bl.... ssf     dfggg   ff>  blah <bl.... ssf     dfggg   ff>
blah <b.... ssf      ghhjj fhf>

$ ./python.py
blah  <hh >t blah
blah <hh >t  blah <hh >t
blah <hh >t
七色彩虹 2024-08-24 22:54:01

听起来像是“re”模块的一项工作,尽管您可以只使用一个 re.sub() 行,但这里有一个小示例函数。

使用“re”模块,一个简单的 re.sub 应该可以解决问题:

import re

def subit(msg):
    # Use the below if the string is multiline
    # subbed = re.compile("(<.*?>)" re.DOTALL).sub("(<hh  >t", msg)
    subbed = re.sub("(<.*?>)", "<hh  >t", msg)
    return subbed

# Your messages bundled into a list
msgs = ["blah  <blah.... ssf  ff> blah",
        "blah <bl.... ssf     dfggg   ff>  blah <bl.... ssf     dfggg   ff>",
        "blah <b.... ssf      ghhjj fhf>"]

# Iterate the messages and print the substitution results
for msg in msgs:
    print subit(msg)

我建议查看“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:

import re

def subit(msg):
    # Use the below if the string is multiline
    # subbed = re.compile("(<.*?>)" re.DOTALL).sub("(<hh  >t", msg)
    subbed = re.sub("(<.*?>)", "<hh  >t", msg)
    return subbed

# Your messages bundled into a list
msgs = ["blah  <blah.... ssf  ff> blah",
        "blah <bl.... ssf     dfggg   ff>  blah <bl.... ssf     dfggg   ff>",
        "blah <b.... ssf      ghhjj fhf>"]

# Iterate the messages and print the substitution results
for msg in msgs:
    print subit(msg)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文