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で「Pythonの正規表現で設定できるフラグの一覧」の動作を確認してみた

Posted at

概要

Pythonで「Pythonの正規表現で設定できるフラグの一覧」の動作を確認してみました。
以下のページを参考にしました。

実装

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

sample.py
import re
pattern = re.compile(r'\w{4}')
print(bool(pattern.search('ABCD')))
print(bool(pattern.search('花鳥風月')))
pattern = re.compile(r'\w{4}', re.ASCII)
print(bool(pattern.search('ABCD')))
print(bool(pattern.search('花鳥風月')))

import re
pattern = re.compile(r'flower')
print(bool(pattern.search('flower')))
print(bool(pattern.search('Flower')))
print(bool(pattern.search('FLOWER')))
pattern = re.compile(r'flower', re.IGNORECASE)
print(bool(pattern.search('flower')))
print(bool(pattern.search('Flower')))
print(bool(pattern.search('FLOWER')))

import re
msg = """Today we're studying JavaScript.
Tomorrow will study the TypeScript."""
pattern = re.compile(r'\w{4}Script\.$')
result = pattern.findall(msg)
print(result)
pattern = re.compile(r'\w{4}Script\.$', re.MULTILINE)
result = pattern.findall(msg)
print(result)

import re
msg = """彼の住所は東京都中央区
日本橋です。"""
pattern = re.compile(r'<span>.*</span>')
print(bool(pattern.search(msg)))
pattern = re.compile(r'<span>.*</span>', re.DOTALL)
print(bool(pattern.search(msg)))

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

$ python3 sample.py 
True
True
True
False
True
False
False
True
True
True
['TypeScript.']
['JavaScript.', 'TypeScript.']
False
False

まとめ

何かの役に立てばと。

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?