Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

regex - Python regular expression to filter list of strings matching a pattern

I use R a lot more and it is easier for me to do it in R:

> test <- c('bbb', 'ccc', 'axx', 'xzz', 'xaa')
> test[grepl("^x",test)]
[1] "xzz" "xaa"

But how to do it in python if test is a list?

P.S. I am learning python using google's python exercise. And I prefer using regression expression.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

In general, you may use

import re                                  # Add the re import declaration to use regex
test = ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] # Define a test list
reg = re.compile(r'^x')                    # Compile the regex
test = list(filter(reg.search, test))      # Create iterator using filter, cast to list 
# => ['xzz', 'xaa']

See the Python demo.

USAGE NOTE:

  • re.search finds the first regex match anywhere in a string and returns a match object, otherwise None
  • re.match looks for a match only at the string start, it does NOT require a full string match. So, re.search(r'^x', text) = re.match(r'x', text)
  • re.fullmatch only returns a match if the full string matches the pattern, so, re.fullmatch(r'x') = re.match(r'x') = re.search(r'^x').

If you wonder what the r'' prefix means, see Python - Should I be using string prefix r when looking for a period (full stop or .) using regex? and Python regex - r prefix.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...