Regexを使用して、一連の単語をPythonで完全に一致させたいと思います。 静的には可能ですが、動的なマッチングの仕方についてはわかりません。RegexをPythonで動的に使用する方法
staticメソッド
import re
print(re.search(r'\bsmaller than or equal\b', 'When the loan amount is smaller than or equal to 50000'))
私はリストでシーケンス全体を照合することによって、動的に同じことをやろうとしています。ここ
は、以下のコードの断片である:
import re
list_less_than_or_equal = ['less than or equal', 'lesser than or equal', 'lower than or equal', 'smaller than or equal','less than or equals', 'lesser than or equals', 'lower than or equals', 'smaller than or equals', 'less than equal', 'lesser than equal', 'higher than equal','less than equals', 'lesser than equals', 'higher than equals']
for word in list_less_than_or_equal:
print(re.search(r'\b'+word+'\b', 'When the loan amount is smaller than or equal to 50000'))
これは出力としてNone
印刷します。
単語のシーケンス全体を動的に照合するにはどうすればよいですか?
あなたの方法論は正しいですが、re.quoteは、属性のエラーが発生します。 – User456898
申し訳ありませんが、 're.escape'である必要があります。更新しました。 – kennytm
それは今適切です。 – User456898