Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get words between specific words in a Python string

I’m working on getting the words between certain words in a string.

Find string between two substrings Referring to this article, I succeeded in catching words in the following way.

s = 'asdf=5;iwantthis123jasd'
result = re.search('asdf=5;(.*)123jasd', s)
print(result.group(1))

But in the sentence below it failed.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

s = '''        <div class="prod-origin-price ">
        <span class="discount-rate">
            4%
        </span>
            <span class="origin-price">'''


result = re.search('<span class="discount-rate">(.*)</span>', s)
print(result.group(1))

I’m trying to bring ‘4%’. Everything else succeeds, but I don’t know why only this one fails.
Help

>Solution :

Try this (mind the white spaces and new lines)

import re
s = '''        <div class="prod-origin-price ">
        <span class="discount-rate">
            4%
        </span>
            <span class="origin-price">'''


result = re.search('<span class="discount-rate">\s*(.*)\s*</span>', s)
print(result.group(1))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading