In Java the regular expression \Q\s\E will match the literal string "\s" and not whitespace because \Q and \E quote the part of the regex inside them. For quoting a whole string conveniently and with care of the edge-case that the string contains an \E, the Pattern.quote-method exists.
Java even has another way of achieving 'regex-methods without regex-special-characters', and that is to pass Pattern.Literal as a flag when compiling the regex.
With python I can not find any equivalent of Pattern.quote nor do I find an equivalent for the underlying mechanism of \Q and \E. I also see no equivalent of the Pattern.Literal-flag. What are my options here?
\Q\s\Eis actually a valid PCRE (Perl Compatible Regular Expression). It should work as is and works correctly in Perlperl -le '$_=q"\s";/\Q\s\E/ ? print "match" : print "no match"';If it doesnt work that means Python did not implement PCRE regular expressions correctly. If\Q\s\Edoesnt work in Python, just manually escape the\sas\\s.perl -le '$_=q"\s";/\\s/ ? print "match" : print "no match"';also works for me. Python should have implemented PCRE exactly. Also make sure you are assigning the string with single quotes instead of double i.e.'\s'and not"\s".\nwould be the literal string\ninstead of a newline. But double quoted strings are interpolated, so\nwould be a newline. I guess python doesnt do that and you would have to escape it with\\nfor the string to interpolate to\n. If python regular expressions are not PCRE you will eventually run into the problem of non-standard regular expressions. Where you will have to completely rewrite all your regular expressions for each language. Sucks. PCRE kind of alleviates this. I seepython-pcremight work.