Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

regex - How to understand regular expression with python?

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+)sw+s(AQ0FS0)', str)
print tupla1

tupla2 = re.findall(r'(w+)sw+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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
^([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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.5k users

...