Files
codeql/python/ql/src/Expressions/Regex/BackspaceEscape.qhelp
2018-11-19 15:10:42 +00:00

41 lines
1.2 KiB
XML

<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
The meaning of the <code>\b</code> escape sequence inside a regular expression depends on its
syntactic context: inside a character class, it matches the backspace character; outside of a
character class, it matches a word boundary. This context dependency makes regular expressions
hard to read, so the <code>\b</code> escape sequence should not be used inside character classes.
</p>
</overview>
<recommendation>
<p>
Replace <code>\b</code> in character classes with the semantically identical escape sequence <code>\x08</code>.
</p>
</recommendation>
<example>
<p>
In the following example, the regular expression contains two uses of <code>\b</code>: in the
first case, it matches a word boundary, in the second case it matches a backspace character.
</p>
<sample src="BackspaceEscape.py" />
<p>
You can make the regular expression easier for other developers to interpret, by rewriting it as <code>r"\b[\t\x08]"</code>.
</p>
</example>
<references>
<li>Python Standard Library: <a href="https://docs.python.org/library/re.html">Regular expression operations</a>.</li>
</references>
</qhelp>