假设我有以下字符串:
string = "** Hunger is the physical sensation of desiring food.
<br> Your Hunger Level: Very Hungery<br> Food You Crave: Tomato<br/><br/>"
我希望能够提取出“你的饥饿”和“番茄”。假设无论插入什么特殊字符,我都知道“你的饥饿程度:”和“你渴望的食物”将始终保持不变。
"Your Hunger Level:" could be: "Very Hungry", "Hungry", "Not So Hungry"
"Food You Crave:" could be: "Tomato", "Rice and Beans", "Corn Soup"
如何使用正则表达式来匹配它?我尝试了以下方法,但没有任何运气......
m = re.match('(.*)([ \t]+)?Your Hunger Level:([ \t]+)?(?P<hungerlevel>.*)(.*)Food You Crave:([ \t]+)?(?P<foodcraving>.*).*', string)
注意:该字符串似乎有很多转义字符,如下所示:
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>"