简体   繁体   中英

How to use regular expression to retrieve specific text in Python

So I'm trying to retrieve all male members from a name list, it looks something like this: AB(male) CDE(male) F(male) G

All strings are separated with space. The name list is saved as a txt file: name.txt

I would like to have Python to read in name.txt and retrieve all males from the list, then print them out (in this case BE and F).

How do I use regular expression to achieve that? Thanks!

我只是给出正则表达式, regex = r"(\\w+)\\(male\\)"

It's apparently some data. Why are you storing and retrieving it from a text file?

If it's some temp data being stored in a text file maybe change the formatting and specify both 'Male' and 'Female' and also one entry per line so you can loop through the file?

That'll be more systematic.

So all you'll have to do is look for a string match for 'Male' in every line and select that line to print.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM