次のコードがあります。
pattern = "something.*\n" #intended to be a regular expression
fileString = some/path/to/file
numMatches = len( re.findall(pattern, fileString, 0) )
print "Found ", numMatches, " matches to ", pattern, " in file."
ユーザーがパターンに含まれる '\n' を確認できるようにしたい。現時点では、パターンの '\n' は画面に改行を書き込みます。したがって、出力は次のようになります。
Found 10 matches to something.*
in file.
そして私はそれが欲しい:
Found 10 matches to something.*\n in file.
はい、 pattern.replace("\n", "\n") は機能します。しかし、\t、\e などを含むすべての形式のエスケープ文字を出力したいのですが、助けていただければ幸いです。