LoginSignup
0
0

More than 1 year has passed since last update.

はじめに

移植やってます。

re.sub (Python)

import re
s = '1A2B3C'
print(re.sub(r'[A-Z]', '', s))
print(re.sub(r'[A-Z]', '', s, count=1))

# 123
# 12B3C

正規表現で一致した文字列を置換します。
countオプションで置換する回数を指定できます。
https://docs.python.org/ja/3.7/library/re.html#re.sub

gsub (Ruby)

s = '1A2B3C'
puts s.gsub(/[A-Z]/, '')
puts s.sub(/[A-Z]/, '')

# 123
# 12B3C

Rubyの場合、すべて置換するgsub/gsub!と最初に一致する文字列を置換するsub/sub!があります。

メモ

  • Python の re.sub を学習した
  • 百里を行く者は九十里を半ばとす
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