如何使用python了解正则表达式? [英] How to understand regular expression with python?

查看:219
本文介绍了如何使用python了解正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的python。任何人都可以帮助我,如何创建一个正则表达式给出一个这样的字符串列表:

  test_string =pero pero CC 
tan tan RG
安提瓜防御AQ0FS0
que que CS
segúnsegúnSPS00
mi mi DP1CSS
madre madre NCFS000

如何返回一个这样的元组:

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

我想返回它是与test_string相关联的标签,这是我所做的:

 # - 编码:utf-8  -  
import re


#str =pero pero CC\
tan tan RG\
antigua antiuo AQ0FS0\
que que CS\
segúnsegúnSPS00\
mi mi DP1CSS\
madre madre NCFS000

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

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

输出如下:


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


此输出的问题是如果我沿着 test_string 我需要保存ord呃或发生(即只有当它们具有以下顺序时才可以打印一个元组:AQ0FS0和NCFS000,换句话说:女性形容词,女性名词)。

解决方案

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

不要真的知道这个选择的基础,但是你仍然可以这样做。请抓住抓取。不要忘记设置标志 g m 。见演示。



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


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).

解决方案

^([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

这篇关于如何使用python了解正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆