-1

I'm new to RegEx. I am using python to go through a web page and pick out certain text. I have been able to pick out part of what I need with some extra character attached. In the example below I am trying to get this expression: "Need This"

import re

test = '<area alt=Need This <span class=;viewot;>view 1</span>||tin view:<br /> ' \
       '<div class=sadfca3 24swcdsa c4566 54dscz>' \
       '<span class=asafwef1 41sd fd3532 safwef>' \
        '<img class=sfecs 234af wefw47 5awef>' \
        '</span> ' \
        '<span class=sad536 fwfad23 4s214 fsadfw>' \
        '<img class=&we234 fsafsdf 2323 asdfsd>' \
        '</span>' \
        '<span class=afasui2 34 ewiasd23 4fjlwe;>' \
        '<img class=sfawejac2 42jk hewwef32 4uafasd>' \
        '</span> ' \
        '<span class=gdfjuia w8 aw ijfaw a909>' \
        '<img class=asfwejhjdkh f 8sd 8 awiosa;f98a 8a' \
        '</span> <div class=afkj waj 98u2oi kjaf09></div>" href="jkhafu.php">'

print("findall")
print(re.findall(r'<area alt=?.*<span class=', str(test), re.I|re.M))
print("finditer")
print(re.finditer(r'<area alt=+.*<span class=', str(test), re.I|re.M))
print("match")
print(re.match(r'<area alt=+.*<span class=', str(test), re.I|re.M))
print("search")
print(re.search(r'<area alt=+.*<span class=', str(test), re.I|re.M))
print("split")
print(re.split(r'<area alt=+.*<span class=', str(test), re.I|re.M))

re.match and re.seach get close to what I am needing. Here is the result from the above example:

findall
['<area alt=Need This <span class=&quot;view&quot;>view 1</span>||time to spend in view:<br /> <div class=sadfca3 24swcdsa c4566 54dscz><span class=asafwef1 41sd fd3532 safwef><img class=sfecs 234af wefw47 5awef></span> <span class=sad536 fwfad23 4s214 fsadfw><img class=&we234 fsafsdf 2323 asdfsd></span><span class=afasui2 34 ewiasd23 4fjlwe;><img class=sfawejac2 42jk hewwef32 4uafasd></span> <span class=']
finditer
<callable_iterator object at 0x00493750>
match
<_sre.SRE_Match object; span=(0, 405), match='<area alt=Need This <span class=&quot;view&quot;>v>
search
<_sre.SRE_Match object; span=(0, 405), match='<area alt=Need This <span class=&quot;view&quot;>v>
split
['', 'gdfjuia w8 aw ijfaw a909><img class=asfwejhjdkh f 8sd 8 awiosa;f98a 8a</span> <div class=afkj waj 98u2oi kjaf09></div>" href="jkhafu.php">']

How can I use RegEx with python 3.4 to only get "Need This" from the string named test in the example above?

Any help would be greatly appreciated!

user908759
  • 1,355
  • 8
  • 26
  • 48
  • Relevant if you're ever doing anything more complicated with html parsing: http://stackoverflow.com/a/1732454/406772 – Sajjan Singh Sep 02 '14 at 23:17
  • The HTML you have is not actually valid. Share the link to the webpage, or provide the relevant web page html as it is. – alecxe Sep 02 '14 at 23:17

2 Answers2

3

Use a lookbehind and lookahead assertion,

(?<=area alt=).*?(?=\s+<span class=)

Code:

>>> m = re.search(r'(?<=area alt=).*?(?=\s+<span class=)', test).group()
>>> m
'Need This'
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
2

You can use this regex:

area alt=([\w\s]+)<

Working demo

enter image description here

The code is:

import re
p = re.compile(ur'area alt=([\w\s]+)<')
test_str = u"YOUR TEXT HERE"
m = re.match(p, test_str)
print m.group(1)
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123