用于在

时间:2016-07-28 11:16:01

标签: python regex

这是我的字符串:

  

年龄:成人/儿童性别:男/女年龄范围:3 - 5岁/ 5 - 8岁/ 8 - 12岁/ 12岁及以上产品类型:服装角色:动物和放大器;昆虫材料:聚酯主题:动物年龄开始:3岁年终:成人特征: - 通过斑马口看到的面孔。 - 前部有拉链封口,后部有尾部。 - 套装包括:连身衣和头罩。 - 动物收藏。年龄: - 成人/孩子。性别:男/女。年龄组:-3 - 5岁/ 5 - 8岁/ 8 - 12岁/ 12岁及以上

我想只捕捉python正则表达式的粗体部分。但我无法做到。我使用这个正则表达式,但不太可能工作。我的正则表达式是:

\bage[a-z]?\b.*\d+\s(?:years[a-z]?|yrs|month[a-z]+)

这是一个奇怪的答案,捕捉不需要的字符串。

3 个答案:

答案 0 :(得分:0)

您可以使用re.search()尝试此模式:

import re

string = 'age: adult/child  gender: male/female  age range: 3 - 5 years/5 - 8 years/8 - 12 years/12 years and up  product type: costume  character: animals & insects  material: polyester  theme: animal  age start: 3 years  age end: adult features:  -face is seen through the mouth of the zebra.  -zipper closure in the front and a tail in the back.  -set includes: jumpsuit and head mask.  -animal collection.  age: -adult/child.  gender: -male/female.  age range: -3 - 5 years/5 - 8 years/8 - 12 years/12 years and up'
match = re.search(r'(age range:.*?)  ', string)
if match:
    print(match.group(1))

输出:

age range: 3 - 5 years/5 - 8 years/8 - 12 years/12 years and up

这依赖于假设每个数据项由两个空格分隔,如给定字符串所示。模式表示匹配字符串age match:后跟零个或多个字符(非贪婪),后跟恰好2个空格。

答案 1 :(得分:0)

您可以使用以下内容:

\bage range:\s*(?:\d+\s*-\s*\d+\s*y(?:ea)?rs/)+\d+\s*y(?:ea)?rs and up\b

请参阅Demo

答案 2 :(得分:0)

如果"产品类型"始终关注您想要的字符串,然后您可以使用lookahead assertion

>>> r = re.search(r'(age range:.*?)(?= product type)', s)
>>> r.group(1)
'age range: 3 - 5 years/5 - 8 years/8 - 12 years/12 years and up'