Home > Blockchain >  Get words between specific words in a Python string
Get words between specific words in a Python string

Time:08-13

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.

s = '''        <div >
        <span >
            4%
        </span>
            <span >'''


result = re.search('<span >(.*)</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

CodePudding user response:

Try this (mind the white spaces and new lines)

import re
s = '''        <div >
        <span >
            4%
        </span>
            <span >'''


result = re.search('<span >\s*(.*)\s*</span>', s)
print(result.group(1))

CodePudding user response:

Use re.DOTALL flag for matching new lines:

result = re.search('<span >(.*)</span>', s, re.DOTALL)

Documentation: https://docs.python.org/3/library/re.html

  • Related