Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用通配符替换python搜索_Python_Regex_Search_Replace_Wildcard - Fatal编程技术网

使用通配符替换python搜索

使用通配符替换python搜索,python,regex,search,replace,wildcard,Python,Regex,Search,Replace,Wildcard,有点困惑。。但是尝试使用通配符进行搜索/重新定位 如果我有类似于: <blah.... ssf ff> <bl.... ssf dfggg ff> <b.... ssf ghhjj fhf> 我已经试过了 name=re.sub("</s[^>]*\">"," foo ",name) name=re.sub(“]*\”>,“foo”,name) 但是我错过了一些东西 想法…感谢您参见非常实用的Python

有点困惑。。但是尝试使用通配符进行搜索/重新定位

如果我有类似于:

 <blah.... ssf  ff>
 <bl.... ssf     dfggg   ff>
 <b.... ssf      ghhjj fhf>
我已经试过了

name=re.sub("</s[^>]*\">"," foo ",name) 
name=re.sub(“]*\”>,“foo”,name)
但是我错过了一些东西


想法…感谢您参见非常实用的Python手册,或是第5.2节“搜索和替换”中更实用的方法。

无需使用正则表达式

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)
对于打开的行(“文件”):
如果行中有“”:
s=line.rstrip().split(“>”)
对于n,枚举中的i:
如果“听起来像是“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)
重新导入
def subit(msg):
#如果字符串是多行的,请使用下面的
#subbed=re.compile(“()”re.DOTALL).sub(“(t”,msg)
subbed=re.sub(“()”,“t”,msg)
返回子床
#您的邮件被捆绑到一个列表中
msgs=[“诸如此类”,
“胡说八道”,
“废话”]
#迭代消息并打印替换结果
对于msg中的msg:
打印subit(msg)

我建议看一下“re”模块的文档,它有很好的文档记录,可能会帮助您实现更准确的文本操作/替换。

像这样,使用regex怎么样

import re

YOURTEXT=re.sub("<b[^>]*>","<hh >t",YOURTEXT)
重新导入
YOURTEXT=re.sub(“]*>”,“t”,YOURTEXT)

Regex是这里最简单的方法。
s/]*>/t/g
这是一个很好的快速而肮脏的解决方案,但也不是很好的扩展性,因为它也不会在
之后检查
b
,我同意。没有来自OP的很多其他信息。此外,如果OP真的在解析完整的HTML(或XML?),甚至建议不要使用Regex:)六羟甲基三聚氰胺六甲醚。。。缺少一些东西。。。我最初的示例文本是:Soo ChoiLONGEDITBOX“>Apryl Berney Soo ChoiLONGEDITBOX”>Joel Franks Joel FranksGEDITBOX”>Alexander Yamato,我试图得到Soo Choi foo Apryl Berney Soo Choi foo Joel Franks Joel Franks foo Alexander Yamato我尝试了name=re.sub(“]*\”>,“foo”,name)的派生词但是我错过了一些东西。。。想法…你是否每件事都使用函数?
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
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)
import re

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