开发者

How to search for a particular string with in a regex group with in a single regex in Python?

I am trying to match the word 'noun' with in the last group of my regex.

So far I have:

tags = 'motocykl mutka 1 motorcy开发者_StackOverflow中文版cle bike moped 0 transportation openair noun'

print re.search('(?P<pol>\D+)(?P<d1>\d)(?P<eng>\D+)(?P<d2>\d)(?P<end>\D+)', tags).group('end')

All I get is a string which is that last group:

transportation openair noun

I need to just get:

noun

UPDATE:

I forgot to mention that 'noun' will not be showing up as the last word in some strings I will be running the regex against. For example:

tags = 'dźwig 1 crane 0 noun construction vehicle'
tags = 'trycykl 1 tricycle 0 child noun transportation'

Any ideas on how to do this with in a single regex?


Not sure what your tags mean but \D+? should match "transportation openair" and the [a-zA-Z] will match the last word (noun):

^(?P<pol>\D+)(?P<d1>\d)(?P<eng>\D+)(?P<d2>\d)\D+?(?P<end>[a-zA-Z]+)$


Your problem is that you are matching with \D+, which will match multiple words including spaces. It makes perfect sense that you are getting the last group of words.

So you need to make your last group only match non-whitespace characters, and before your last group match on a whitespace character.

Here's a pattern that matches "transportation openair" in a group called "category" and correctly matches "noun" in the group "end". Because we used the non-greedy + in matching category, we need a $ to anchor the end group to actually be the last word in the string.

re.match(r'(?P<pol>\D+)(?P<d1>\d)(?P<eng>\D+)(?P<d2>\d)(?P<category>\D+?)\W+(?P<end>\w+)$', tags).group('end')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜