101
81

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonの正規表現で特殊記号をすべて闇に葬り去りたいとき

Posted at

記号がいらない

/ * # %
など、スクレイピング・自然言語処理においては記号はいらない場合があります。

Pythonのreモジュールで一括削除をします。

import re
code_regex = re.compile('[!"#$%&\'\\\\()*+,-./:;<=>?@[\\]^_`{|}~「」〔〕“”〈〉『』【】&*・()$#@。、?!`+¥%]')

txt = input().rstrip()
cleaned_text = code_regex.sub('', txt)
print(cleaned_text)

[]に入っている記号のどれかに一致してしたとき削除してくれるため、この中にいらない文字を入れて削除をします。
全角の記号も強引に打ち込んで闇に葬り去りましょう。

101
81
2

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
101
81

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?