简体   繁体   中英

How to match the following with regex in python?

Assume I have the following string:

string = "** Hunger is the physical sensation of desiring food.                                      

<br>         Your Hunger Level: Very Hungery<br> Food You Crave: Tomato<br/><br/>"

I want to be able to extract out "Your Hunger" and "Tomato". Assume that regardless of what special characters are inserted, I know for a fact that "Your Hunger Level:" and "Food You Crave" will always be constant.

"Your Hunger Level:" could be: "Very Hungry", "Hungry", "Not So Hungry"
"Food You Crave:" could be: "Tomato", "Rice and Beans", "Corn Soup"

How do I use a regular expression to match this? I tried the following, but am not getting any luck...

m = re.match('(.*)([ \t]+)?Your Hunger Level:([ \t]+)?(?P<hungerlevel>.*)(.*)Food You Crave:([ \t]+)?(?P<foodcraving>.*).*', string)                

NOTE: The string appears to have a lot of escape characters indicated below:

string = "** Hunger is the physical sensation of desiring food. <br>\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\tYour Hunger Level:
Very Hungry \n\t\t\t\t\t\t\t\t<br>\n\t\t\t\t\t\t\t\tFood You Crave: Tomato \n\t\t\t\t\t\t</br>"

I'd go for:

print [map(str.strip, line.split(':')) for line in re.split('<.*?>', string) if ':' in line]
# [['Your Hunger Level', 'Very Hungery'], ['Food You Crave', 'Tomato']]

Or, you could make it a dict :

lookup = dict(map(str.strip, line.split(':')) for line in re.split('<.*?>', text) if ':' in line)
print lookup['Your Hunger Level']
# 'Very Hungry'

I definitely agree with using any sort of parser, but the following seems to work. It simply starts after your target word and goes until it hits a < (I do not endorse it for the record, but hopefully it works :) ):

In [28]: import re

In [29]: s = """** Hunger is the physical sensation of desiring food.
<br>         Your Hunger Level: Very Hungery<br> Food You Crave: Tomato<br/><br/>"""

In [31]: m = re.search(r'Your Hunger Level:([^<]*)<br>.*Food You Crave:([^<]*)', s)

In [32]: m.group(1).strip()
Out[32]: 'Very Hungery'

In [33]: m.group(2).strip()
Out[33]: 'Tomato'

The strip() is to trim whitespace - not sure what the setup of your string is, but this is conservative so that it handles cases where there is no space between the colon and the text. Also, I would recommend not using Python keywords as variable names ( string , in this case) - it will make things easier for you in the long run :)

  1. First, parse the HTML with a parser. There are many at your disposal, eg beautiful soup, lxml.
  2. Second, search the document for <br> tags.
  3. Third, do a search over the text of the tags for the text that you want, and return that tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM