How to understand regular expression with python?

2024/11/7 18:01:58

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 RGantigua antiguo AQ0FS0que 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 tupla1tupla2 = 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).

Answer
^([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://en.xdnf.cn/q/120713.html

Related Q&A

How do I reverse words in a string with Python [duplicate]

This question already has answers here:Reversing words in a Python string (including punctuation)(5 answers)Closed last month.I am trying to reverse words of a string, but having difficulty, any assist…

Reading input files and writing into output files - Python

I have an input file (input.txt) with the following information:Number of students (first line) Number of test scores (second line) list of student names and scoresSo the text file looks something like…

Get strings list in python with regex [duplicate]

This question already has answers here:Split string with multiple-character delimiter(3 answers)Closed 6 years ago.I want extract strings from this text with regex:~ZCC0ZAF~World~AAEef~RZgthAD~AAjaKNed…

python static code analysis tools - code analysis (preliminary research question) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 3…

how can I get all post of users in django [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python 3: Getting IndexError: list index out of range on shuffle method

Im building a blackjack command line game and Ive run into a snag. The shuffle feature on my deck class object keeps coming up with IndexError: list index out of range on line 29. It is a sporadic bug…

How to encrypt a file

Just trimmed this down big timeI have an overall assignment that must read a file, encrypt it and then write the encrypted data to a new file.what ive tried is this:filename=input("Enter file name…

Any Idea on how Should I analyze this Algorithm? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Syntax error ; multiple statements found while

Why is there this syntax error Multiple statements found while compiling a single statement given when I run this code? Answer and help will be super appreciated for this python newbie here

No module named PyPDF2._codecs, even after already installed

I have installed PyPDF2==2.3.0, but I still get the error below when I import PyPDF2. The error message is:ModuleNotFoundError: No module named PyPDF2._codecs