1
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?

Pythonで「文字列の全体がパターンとマッチするか調べる(Pattern.fullmatch)」の動作を確認してみた

Posted at

概要

Pythonで「文字列の全体がパターンとマッチするか調べる(Pattern.fullmatch)」の動作を確認してみました。
以下のページを参考にしました。

実装

以下のファイルを作成しました。

sample.py
import re

def checkMatch(msg, pat):
    pattern = re.compile(pat)
    result = pattern.fullmatch(msg)
    if result :
        print(result.group(0))
    else :
        print('Don\'t matched')

checkMatch('東京都港区赤坂', r'東.*区')
checkMatch('東京都港区赤坂', r'東.*坂')

import re

def checkMatch(msg, pattern, start, end):
    result = pattern.fullmatch(msg, start, end)
    if result :
        print(result.group(0))
    else :
        print('Don\'t matched')

msg = '東京都港区赤坂'
pattern = re.compile(r'港区')

checkMatch(msg, pattern, 0, 5)
checkMatch(msg, pattern, 3, 5)

以下のコマンドを実行しました。

$ python3 sample.py 
Don't matched
東京都港区赤坂
Don't matched
港区

まとめ

何かの役に立てばと。

1
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
1
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?