Rhys
Rhys

Reputation: 5282

Python 3, Listing all file names found in string

The following string example uses .swf

<name="echo" value="2010a.swf"><ie=tango v="bolder.swf"><val=="backstreetboys+AreBack.swf">

What is the best method to extract all .swf names using their corresponding =" string as the hook.

The first code I considered was something like this:

cnt = 0
holdResults = []
while bla.find('.swf', cnt) != -1:
    holdResults.insert(len(holdResults), bla[bla.find('.swf', cnt):-1])
    cnt = bla.find('.swf', cnt)
    cnt = cnt+1

print(holdResults)

The roadblock I'm at with the above code is the :-1]) part ...

I could loop backwards one character at a time until finding the corresponding =" ... but this doesn't seem ideal or fast, especially when customizing the =' hook to something else or lengthen it.

Is there a faster more effective way of doing this?

Thanks!

Upvotes: 0

Views: 46

Answers (1)

Aditya Gune
Aditya Gune

Reputation: 520

You can use Python regexes to find all matches instead of iterating over the string:

import re

#your pattern here
pattern = r"\=\"[a-zA-Z0-9\+\!\@\#\$\%\^\&\*\(\)\_]*\.swf\""

#string to search
test_str = "<name=\"echo\" value=\"2010a.swf\"><ie=tango v=\"bolder.swf\"><val==\"backstreetboys+AreBack.swf\">"

#use re.findall() to get all matches
m = re.findall(pattern, test_str)

#print the matches
if m:
    print("Found matches:")
    for i in enumerate(m):
        print(i)

This is a good regex tester to get the correct pattern: https://regex101.com/

Upvotes: 1

Related Questions