简体   繁体   中英

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.

This regex, on the other hand, matches itself, but also much more

^a.*3$

matches: "a3", "afoo3", "a.*3". This could not be replaced with an input =="a.*3" string comparison.

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:

>>> s = "abc123"
>>> re.escape(s) == s
True
>>> s = "a.*3"
>>> re.escape(s) == s
False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM