我使用 R 的次数更多,用 R 做起来更容易:
> test <- c('bbb', 'ccc', 'axx', 'xzz', 'xaa')
> test[grepl("^x",test)]
[1] "xzz" "xaa"
但是如果 test
是一个列表,如何在 python 中做呢?
附言我正在使用谷歌的 python 练习学习 python,我更喜欢使用正则表达式。
最佳答案
一般来说,你可以使用
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']
或者,反转结果并获取所有与正则表达式不匹配的项目:
list(filter(lambda x: not reg.search(x), test))
# >>> ['bbb', 'ccc', 'axx']
参见 Python demo .
使用说明:
-
re.search
在字符串中找到第一个正则表达式匹配 anywhere 并返回一个匹配对象,否则None
-
re.match
仅在字符串开头查找匹配项,它不需要完整的字符串匹配项。所以,re.search(r'^x', text)
=re.match(r'x', text)
-
re.fullmatch
仅当完整字符串与模式匹配时才返回匹配项,因此,re.fullmatch(r'x')
=re.match(r'x\Z')
=re.search(r'^x\Z')
.
如果您想知道 r''
前缀的含义,请参阅 Python - Should I be using string prefix r when looking for a period (full stop or .) using regex?和 Python regex - r prefix .
关于python - 用于过滤与模式匹配的字符串列表的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15403021/