Home > Mobile >  Saving regex matches on a list
Saving regex matches on a list

Time:05-30

I have a file of accounts that looks like this


<A0001><$241><div ><img  title="" src="/static/assets/images/thumb/1535.png"width="64" height="64"></div><1231>
<div ><img  title="" src="/static/assets/images/thumb/1510.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/1403.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/1388.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/1323.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/1322.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/1172.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/1069.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/0966.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/0796.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/1430.png"width="64" height="64"></div>


<A0002><$111><div ><img  title="" src="/static/assets/images/thumb/1535.png"width="64" height="64"></div><3112>
<div ><img  title="" src="/static/assets/images/thumb/1510.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/1403.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/1388.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/1323.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/1322.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/1172.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/1069.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/0966.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/0796.png"width="64" height="64"></div>
<div ><img  title="" src="/static/assets/images/thumb/1430.png"width="64" height="64"></div>
...

As you can see the images aren't a fixed number, they are different from one to another. I already have a script that uses regex to find the image's name, but how can I find all the images on the file and save them to a list with every index having all the image names of the specific account. Like this


List = [

'src="resources/images/thumb/1129.png"
src="resources/images/thumb/1129.png"
src="resources/images/thumb/1129.png"
src="resources/images/thumb/1129.png"
src="resources/images/thumb/1129.png"
src="resources/images/thumb/1129.png"'

,

'src="resources/images/thumb/1129.png"
src="resources/images/thumb/1129.png"
src="resources/images/thumb/1129.png"
src="resources/images/thumb/1129.png"'


,
'src="resources/images/thumb/1129.png"
src="resources/images/thumb/1129.png"
src="resources/images/thumb/1129.png"
src="resources/images/thumb/1129.png"
src="resources/images/thumb/1129.png"
src="resources/images/thumb/1129.png"
src="resources/images/thumb/1129.png"'

] # And so on


EDITED: I also edited how the file may really look, so the split() method may not work at all, sorry for all the misunderstanding

CodePudding user response:

If I understand you correctly you want to "group" images under specific section. For example:

import re

r1 = re.compile(r"A\d ")
r2 = re.compile(r'src="(.*)"')

out, key = {}, None
with open("your_file.txt", "r") as f_in:
    for line in f_in:
        if r1.match(line):
            key = line.strip()
        elif (m := r2.match(line)) :
            out.setdefault(key, []).append(m.group(1))

print(out)

Prints:

{
    "A0001": [
        "resources/images/thumb/1634.png",
        "resources/images/thumb/1234.png",
        "resources/images/thumb/1145.png",
        "resources/images/thumb/1243.png",
    ],
    "A0002": [
        "resources/images/thumb/1129.png",
        "resources/images/thumb/1235.png",
    ],
}

EDIT: To get only images:

import re

r = re.compile(r'src="(.*)"')

out = []
with open("your_file.txt", "r") as f_in:
    for line in f_in:
        if (m := r.match(line)) :
            out.append(m.group(1))

print(out)

Prints:

[
    "resources/images/thumb/1634.png",
    "resources/images/thumb/1234.png",
    "resources/images/thumb/1145.png",
    "resources/images/thumb/1243.png",
    "resources/images/thumb/1129.png",
    "resources/images/thumb/1235.png",
]
  • Related