2009-07-28 2 views
3

I)は(Pythonの2.6を使用していますし、re.subから[私はと思われるもの]予期しない出力を取得しています誰かがこのPython re.sub()の予期しない出力を説明できますか?

>>> re.sub('[aeiou]', '-', 'the cat sat on the mat') 
'th- c-t s-t -n th- m-t' 
>>> re.sub('[aeiou]', '-', 'the cat sat on the mat', re.IGNORECASE) 
'th- c-t sat on the mat' 

この出力が期待されているものである場合には、その背後にあるロジックは何ですか?

答えて

7

はい、4番目のパラメータはcountであり、flagsではありません。パターンを2度適用するように指示しています(re.IGNORECASE = 2)。あなたはこの質問をしているのでアップグレードした場合re.compile

expression = re.compile('[aeiou]', re.IGNORECASE) 
expression.sub('-', 'the cat sat on the mat') 
+0

はいああ。パラメータの混合。ありがとうございました。 – kjfletch

4

。 Python 2.7以降を使用している場合は、re.compileを使用する必要はありません。 subを呼び出して、名前付き引数を使用してflagsと指定することができます。

>>> import re 
>>> re.sub('[aeiou]', '-', 'the cat sat on the mat', flags=re.IGNORECASE) 
'th- c-t s-t -n th- m-t' 

参考:https://docs.python.org/2/library/re.html#re.sub

0

使用できるフラグを渡すために

関連する問題