Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
正規表現を使用して、文内の各単語を同じ単語に置き換えようとしていますが、引用符で囲みます (単語は文字だけで数字は意味しません)。
たとえば、4 python codeに変換する必要があります4 "python" "code"。
4 python code
4 "python" "code"
しかし、このコードは間違った結果を生成します
>>> import re >>> s = "4 python code" >>> re.sub(r'([a-z]*)', r'"\1"', s) '""4"" "python" "code"'
何か案は?
変化する、
re.sub(r'([a-z]*)', r'"\1"', s)
に
re.sub(r'([a-z]+)', r'"\1"', s)
を使わない別の方法re。
re
s = "4 python code" new = " ".join([item if item.isdigit() else '"{}"'.format(item) for item in s.split()])