[英]How to determine if regex is exact
I'd like to determine if a regex exactly matches against its own contents. 我想确定一个正则表达式是否与其自身内容完全匹配。 My goal is to determine if an arbitrary regex can be replaced by a simple string comparison. 我的目标是确定是否可以通过简单的字符串比较来替换任意正则表达式。
For example, the regex 例如,正则表达式
^abc123$
exactly matches the string "abc123" and nothing else, so we could replace it with an input == "abc123"
string comparison. 与字符串“ abc123”完全匹配,别无其他,因此我们可以将其替换为input == "abc123"
字符串比较。
This regex, on the other hand, matches itself, but also much more 另一方面,此正则表达式可以匹配自身,但还可以匹配更多
^a.*3$
matches: "a3", "afoo3", "a.*3". 匹配:“ a3”,“ afoo3”,“ a。* 3”。 This could not be replaced with an input =="a.*3"
string comparison. 不能用input =="a.*3"
字符串比较来代替。
What is the best approach to determining if a regex only matches a single exact string? 确定正则表达式是否仅匹配单个精确字符串的最佳方法是什么? Is there a complete list of control characters I can look for? 我可以找到控制字符的完整列表吗?
Most programming languages that support regular expressions should have a helper function for escaping any special regex-characters within a string. 大多数支持正则表达式的编程语言都应具有一个辅助函数,用于转义字符串中任何特殊的正则表达式字符。 Just apply that function to the string and see whether the escaped version is the same as the original (without the ^...$
). 只需将该函数应用于字符串,然后查看转义版本是否与原始版本相同(没有^...$
)。
Example in Python: Python中的示例:
>>> s = "abc123"
>>> re.escape(s) == s
True
>>> s = "a.*3"
>>> re.escape(s) == s
False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.