0

EDIT: As already explained in the original question, re.escape() is not a solution for this use case. This is therefore not a duplicate of Escaping regex string in Python. Please read the question before closing as a duplicate!

Java allows the use of \Q and \E to escape blocks of characters, instead of only escaping characters one at a time. Java Docs.

Is there an equivalent for this in Python? I've reviewed the Python re Docs and couldn't find it, but I want to make sure I'm not missing something.

I know there's re.escape() which will automatically escape all non-ASCII characters, but this doesn't work for my use case.

My use case is effectively taking a user-supplied string of format foo<a>bar<b>baz<c> and turning it into a regex that will match foo followed by a number then bar then another number then baz then another number. It would then capture the values of <a>, <b>, and <c>. Also, it's possible that foo, bar and baz could instead be strings containing regex special characters, such as in the case of $.cities[<a>].name.

Java code is something like this:

String key = "foo<a>bar<b>baz<c>";

String keyRegex = "^\\Q" + key + "\\E$";
Matcher m = Pattern.compile("<.*?>").matcher(key);

while (m.find()) {
    keyRegex = keyRegex.replace(m.group(), "\\E([0-9]+)\\Q");
}

keyPattern = Pattern.compile(keyRegex);
John Chrysostom
  • 3,973
  • 1
  • 34
  • 50

0 Answers0