How to understand regular expression with python?

喜你入骨 提交于 2019-12-25 21:11:33

问题


I'm new with python. Could anybody help me on how I can create a regular expression given a list of strings like this:

  test_string =  "pero pero CC 
    tan tan RG
    antigua antiguo AQ0FS0
    que que CS 
    según según SPS00 
    mi mi DP1CSS 
    madre madre NCFS000"

How to return a tuple like this:

> ([madre, NCFS00],[antigua, AQ0FS0]) 

I would like to return the word with it's associated tag given test_string, this is what I've done:

# -- coding: utf-8 --
import re


#str = "pero pero CC " \
   "tan tan RG " \
   "antigua antiguo AQ0FS0" \
    "que que CS " \
    "según según SPS00 " \
    "mi mi DP1CSS " \
    "madre madre NCFS000"

tupla1 = re.findall(r'(\w+)\s\w+\s(AQ0FS0)', str)
print tupla1

tupla2 = re.findall(r'(\w+)\s\w+\s(NCFS00)',str)
print tupla2

The output is the following:

[('antigua', 'AQ0FS0')] [('madre', 'NCFS00')]

The problem with this output is that if I pass it along test_string I need to preserve the "order" or "occurrence" of the tags (i.e. I only can print a tuple if and only if they have the following order: AQ0FS0 and NCFS000 in other words: female adjective, female noun).


回答1:


^([a-zA-Z]+)\s+[a-zA-Z]+\s+([\w]+(?=\d$)\d)

Dont really know the basis for this selection but still you can get it like this.Just grab the captures.Dont forget to set the flags g and m.See demo.

http://regex101.com/r/nA6hN9/38



来源:https://stackoverflow.com/questions/26109602/how-to-understand-regular-expression-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!