LoginSignup
0
0

More than 1 year has passed since last update.

#python で正規表現置換の結果に変数を埋め込む例

Last updated at Posted at 2019-05-19

特に気にすることなく string を扱えば ok

import re
some_var = "is"
re.sub(r"(Alice) (Liddell)","\\1 " + some_var  +  " \\2", 'Alice Liddell')
# 'Alice isLiddell'

re.sub(r"(Alice) (Liddell)","\\1 {is} \\2".format(**{"is":some_var}), 'Alice Liddell')
# 'Alice is Liddell'

re.sub(r"(Alice) (Liddell)", f"\\1 {some_var} \\2", 'Alice Liddell')
#  'Alice is Liddell'

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0