0
0

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 1 year has passed since last update.

[py2rb] re.compile

Last updated at Posted at 2021-12-15

はじめに

移植やってます

re.compile (Python)

import re

msg = '電話番号は080-111-9999です!'
pattern = re.compile(r'(\d{2,4})-(\d{2,4})-(\d{4})')

print(pattern)
print(pattern.search(msg).group(0))

# re.compile('(\\d{2,4})-(\\d{2,4})-(\\d{4})')
# 080-111-9999

compile関数は正規表現オブジェクトを返します。
この例ではピンときませんが、compile(pattern, flags=0)で正規表現オプションを利用することにより複雑な正規表現を簡単にすることも可能です。

// (Ruby)

msg = '電話番号は080-111-9999です!'

pattern = /(\d{2,4})-(\d{2,4})-(\d{4})/

puts pattern
puts pattern.match(msg)

# (?-mix:(\d{2,4})-(\d{2,4})-(\d{4}))
# 080-111-9999

当面は正規表現をゴリゴリと。
[追記]

[追記2]
Rubyの正規表現で便利そうな機能が

メモ

  • Python の re.complie を学習した
  • 道のりは遠そう
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?