Pythonの正規表現を文字列unescaping
-
08-06-2019 - |
質問
うことにより実施string unescaping Python regexとbackreferencesではなさそうしています。このようになっている誤ったのだけど...
>>> import re
>>> mystring = r"This is \n a test \r"
>>> p = re.compile( "\\\\(\\S)" )
>>> p.sub( "\\1", mystring )
'This is n a test r'
>>> p.sub( "\\\\\\1", mystring )
'This is \\n a test \\r'
>>> p.sub( "\\\\1", mystring )
'This is \\1 a test \\1'
思わ\\[char]は[char"が、backreferences Pythonではないがルに従い他の実施います。が誰かを明らかにす?
解決
などのAnders'第えているか。
2.5ものもありま string-escape
エンコードできます:
>>> mystring = r"This is \n a test \r"
>>> mystring.decode('string-escape')
'This is \n a test \r'
>>> print mystring.decode('string-escape')
This is
a test
>>>
他のヒント
も多いかと思いきってのrまたはmiscountedのバックスラッシュ...
"\\n" == r"\n"
>>> import re
>>> mystring = r"This is \\n a test \\r"
>>> p = re.compile( r"[\\][\\](.)" )
>>> print p.sub( r"\\\1", mystring )
This is \n a test \r
>>>
場合には受け付けていませんが何を求められました。
疑いを請求するこ
>>> d = {'n':'\n', 'r':'\r', 'f':'\f'}
>>> p = re.compile(r"[\\]([nrfv])")
>>> print p.sub(lambda mo: d[mo.group(1)], mystring)
This is \
a test \
>>>
興味がある方は学生も十分に意識すべきとトンプソンの 反射光の信頼信頼", 記載のヒーローを採用している。例の説明の持つ信頼のコンパイラにいbootstrappedからマシンコードす。
の考え方は、ちょっと読みに逃れた文字列、unescapeなどいかなる手段によっても、機能、特に欠からPython、なんかインタビューをチェックのための正規表現の場となる残念ながら私な情報をもとに、最新版のバックスラッシュ...
別の実例:
>>> mystring = r"This is \n ridiculous"
>>> print mystring
This is \n ridiculous
>>> p = re.compile( r"\\(\S)" )
>>> print p.sub( 'bloody', mystring )
This is bloody ridiculous
>>> print p.sub( r'\1', mystring )
This is n ridiculous
>>> print p.sub( r'\\1', mystring )
This is \1 ridiculous
>>> print p.sub( r'\\\1', mystring )
This is \n ridiculous
たいと思いま印刷する
This is
ridiculous
する情報をもとに、最新版によるPythonの表現、結果の文字列になります。Pythonの式:
'This is \\n a test \\r'
を表す文字列
This is \n a test \r
ではないかと思いました。を設けてみたら"印刷"の前でそれぞれのp.sub()呼び出しの実され、返される文字列ではなく、Pythonの文字列になります。
>>> mystring = r"This is \n a test \r"
>>> mystring
'This is \\n a test \\r'
>>> print mystring
This is \n a test \r
マーク;二度目の例が必要で毎に逃れた文字がスローされ、配列の最初にすることにより、KeyError場合、エスケープシーケンスがなされ、配列になります。いるものの文字の提供(\vう)、羅列でエスケープシーケンスだと思ったことは、ありませunescape文字列(ファイルを保存するグローバル配列)は思います。類似のPHPが使用 preg_replace_callback()
とラムダの代わりに preg_replace()
, は全く不要です。
とんがったと考える人もいるかもしれないディックが、私は全くいます。この対応はその他の正規表現エンジンをよく使用するのは納得がいかないこなす。
ご回答;の string.decode('string-escape')
機能を精密にいたします。れば、一般の正規表現backreference問題、ご自由ですがそもそもの私の回答の受け入れることとして回答しています。