暂无 |

字符类

字符类提供了只匹配特定字符集中的一个的方法。 字符类是通过将其匹配的字符放置在方括号内而创建的。

例如:

mport re

pattern = r"[aeiou]"

if re.search(pattern, "grey"):
   print("Match 1")

if re.search(pattern, "qwertyuiop"):
   print("Match 2")

if re.search(pattern, "rhythm myths"):
   print("Match 3")

结果:

>>>
Match 1
Match 2
>>>
搜索函数中的模式[aeiou]匹配包含定义的任何一个字符的所有字符串。

1

发表评论

    评价:
    验证码: 点击我更换图片
    最新评论