python - 用于过滤与模式匹配的字符串列表的正则表达式

标签 python regex list subset

我使用 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/

相关文章:

python - 'NoneType' 对象在 OpenLCA 中没有属性 'append'

python - 考虑到所有多边形都是一个分区,如何快速查找包含点的多边形?

python - 有什么方法可以确定 numpy 数组是否是记录/结构数组?

regex - 将旧域重定向到新域的主页

list - 连接列表并将结果返回到第一个参数

python - 如何制作带有\t符号的表格?

php - 正则表达式匹配任何 UTF 字符,不包括标点符号

Python 正则表达式日期 YYYY-MM-DD HH :MM:SS

python - 使用一行多重赋值在列表中交换元素

python - 根据另一个列表中的元素从一个列表中删除元素