1

i have a string like this:

to_search = "example <a>first</a> asdqwe <a>second</a>"

and i want to find both solutions between like this:

list = ["first","second"]

i know that when searching for one solution i should use this code:

import re

if to_search.find("<a>") > -1:
    result = re.search('<a>(.*?)</a>', to_search)
    s = result.group(1)
    print(s)

but that only prints:

first

i tried result.group(2) and result.group(0) but i get the same solution


how can i make a list of all solutions?

problem404
  • 43
  • 1
  • 5

3 Answers3

1

best to use a HTML parser than regex, but change re.search to re.findall

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
flexi
  • 153
  • 1
  • 8
1

Just use:

import re
to_search = "example <a>first</a> asdqwe <a>second</a>"
matches = re.findall(r'<a>(.*?)</a>', to_search)
print(matches)

OUTPUT

['first', 'second']
AnsFourtyTwo
  • 2,480
  • 2
  • 13
  • 33
0
to_search = "example <a>first</a> asdqwe <a>second</a>"
for match in re.finditer("<a>(.*?)</a>", to_search):
    captured_group = match.group(1)
    # do something with captured group
Gabriel
  • 10,524
  • 1
  • 23
  • 28