Python search replace with wildcards

somewhat confused .. but tried to search / repeat 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 lines with, say,

 <hh  >t

any thoughts / comments on how to do this?

thank

update (thanks for the comments!)

Did I miss something...

my original sample text:

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 tried the output from

name=re.sub("</s[^>]*\">"," foo ",name) 

but I missed something ...

thoughts ... thanks

+3
source share
4 answers

. Python , HOWTO 5.2 .

+2

,

import re

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

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
0

"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" , / .

0

Source: https://habr.com/ru/post/1728997/


All Articles