在正则表达式中以任何顺序匹配所有字符 [英] matching all characters in any order in regex

查看:42
本文介绍了在正则表达式中以任何顺序匹配所有字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是正则表达式新手,但我了解如何按顺序匹配正则表达式查询中的任何字符(例如 [abc] 将匹配 a、b 或 c 中的任何一个.此外,我相信abc"将匹配 abc正是).

但是,如何构建一个正则表达式查询,以任何顺序匹配所有字符 abc?例如,我希望它匹配cab"或bracket".我使用 Python 作为我的脚本语言(不确定这是否重要).

解决方案

在 Python 中,我不会为此使用正则表达式,而是使用集合:

<预><代码>>>>chars = set("abc")>>>chars.issubset("括号")真的>>>chars.issubset("鱼")错误的>>>chars.issubset("坏")错误的

正则表达式很有用,但在某些情况下,不同的工具更合适.

I'm a regex newbie, but I understand how to match any characters in a regex query in order (ex. [abc] will match any of a, b or c. Also, I believe "abc" will match abc exactly).

However, how do I construct a regex query that will match all the characters abc in any order? So for example, I want it to match "cab" or "bracket". I'm using Python as my scripting language (not sure if this matters or not).

解决方案

In Python, I wouldn't use a regualar expression for this purpose, but rather a set:

>>> chars = set("abc")
>>> chars.issubset("bracket")
True
>>> chars.issubset("fish")
False
>>> chars.issubset("bad")
False

Regular expressions are useful, but there are situations where different tools are more appropriate.

这篇关于在正则表达式中以任何顺序匹配所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆