marcels93
marcels93

Reputation: 55

Get all substrings between two different start and ending delimiters

I am trying in Python 3 to get a list of all substrings of a given String a, which start after a delimiter x and end right before a delimiter y. I have found solutions which only get me the first occurence, but the result needs to be a list of all occurences.

start = '>'
end = '</'
s = '<script>a=eval;b=alert;a(b(/XSS/.source));</script><script>a=eval;b=alert;a(b(/XSS/.source));</script>'"><marquee><h1>XSS by Xylitol</h1></marquee>'
print((s.split(start))[1].split(end)[0])

the above example is what I've got so far. But I am searching for a more elegant and stable way to get all the occurences.

So the expected return as list would contain the javascript code as following entries:

a=eval;b=alert;a(b(/XSS/.source));
a=eval;b=alert;a(b(/XSS/.source));

Upvotes: 0

Views: 122

Answers (1)

orKa
orKa

Reputation: 121

Looking for patterns in strings seems like a decent job for regular expressions. This should return a list of anything between a pair of <script> and </script>:

import re
pattern = re.compile(r'<script>(.*?)</script>')
s = '<script>a=eval;b=alert;a(b(/XSS/.source));</script><script>a=eval;b=alert;a(b(/XSS/.source));</script>\'"><marquee><h1>XSS by Xylitol</h1></marquee>'
print(pattern.findall(s))

Result:

['a=eval;b=alert;a(b(/XSS/.source));', 'a=eval;b=alert;a(b(/XSS/.source));']

Upvotes: 1

Related Questions