私は文字列を持っています:
s = 'This is a number -N-'
-N-
プレースホルダーを正規表現に置き換えたい:
s = 'This is a number (\d+)'
したがって、後で正規表現として使用s
して、別の文字列と照合できます。
re.match(s, 'This is a number 2')
ただし、スラッシュをエスケープしない正規表現で s を置き換えることはできません。
re.sub('-N-', r'(\d+)', 'This is a number -N-')
# returns 'This is a num (\\d+)'
私がここで間違っていることを教えてください。ありがとう!