茨の道も一歩から

インフラ構築からプログラミング(Python・JavaScript)までITに関するブログです。

79日目:Pythonプログラミング

Pythonプログラミングの講義13日目。

今日の講義も、人工無脳GUI版の機能拡張。内職は、Djangoアプリ制作

【講義内容】

【ワンポイント】

正規表現

import re

pattern = 'Python'
line = 'Python is programming language.'

m = re.match(pattern, line)
if m is not None:
    print(m.group())
else:
    print('No match.')

正規表現サンプル

match

  • string の先頭で 0 個以上の文字が正規表現 pattern にマッチすれば、対応する マッチオブジェクト を返します。文字列がパターンにマッチしなければ None を返します。
import re

pattern = 'Python'
lines = [
         'Python is programming language.',
         'Ruby is programming language.',
         'Ruby, PHP, JavaScript and Python are lightweight language.',
         'Ruby, PHP, JavaScript and Python are lightweight languages. Python is popular these days.',
         'My favorite programming language is Python.',
         'My favorite programming language is JavaScript.'
]

print('***** match *****')

for line in lines:
    m = re.match(pattern, line)
    if m is not None:
        print(m.group())
        print(m)
    else:
        print('Dose not match.')

出力

***** match *****
Python
<_sre.SRE_Match object; span=(0, 6), match='Python'>
Dose not match.
Dose not match.
Dose not match.
Dose not match.
Dose not match.

search

  • string を走査し、正規表現 pattern がマッチを生じさせる最初の場所を探して、対応する マッチオブジェクト を返します。文字列内にパターンにマッチする場所がなければ None を返します。
print('***** search *****')

for line in lines:
    m = re.search(pattern, line)
    if m is not None:
        print(m.group())
        print(m)
    else:
        print('Dose not match.')

出力

***** search *****
Python
<_sre.SRE_Match object; span=(0, 6), match='Python'>
Dose not match.
Python
<_sre.SRE_Match object; span=(26, 32), match='Python'>
Python
<_sre.SRE_Match object; span=(26, 32), match='Python'>
Python
<_sre.SRE_Match object; span=(36, 42), match='Python'>
Dose not match.

findall

  • string 中の pattern による全ての重複しないマッチを、文字列のリストとして返します。 string は左から右へ走査され、マッチは見つかった順で返されます。パターン中に 1 つ以上のグループがあれば、グループのリストを返します。パターンに複数のグループがあればタプルのリストになります。
print('***** findall *****')

for line in lines:
    m = re.findall(pattern, line)
    if m:
        print(m)
    else:
        print('Dose not match.')

出力

***** findall *****
['Python']
Dose not match.
['Python']
['Python', 'Python']
['Python']
Dose not match.

finditer

  • string 中の正規表現 pattern の重複しないマッチ全てに渡る マッチオブジェクト を yield する イテレータ を返します。 string は左から右へ走査され、マッチは見つかった順で返されます。
print('***** finditer *****')

for idx, line in enumerate(lines):
    m = re.finditer(pattern, line)
    print(f'{idx}: {m}')
    for i in m:
        print(f'{idx}: {i.group()}')
        print(f'{idx}: {i.span()}')

出力

***** finditer *****
0: <callable_iterator object at 0x7f0ffe7d30b8>
0: Python
0: (0, 6)
1: <callable_iterator object at 0x7f0ffe7d30f0>
2: <callable_iterator object at 0x7f0ffe7d30b8>
2: Python
2: (26, 32)
3: <callable_iterator object at 0x7f0ffe7d30f0>
3: Python
3: (26, 32)
3: Python
3: (60, 66)
4: <callable_iterator object at 0x7f0ffe7d30b8>
4: Python
4: (36, 42)
5: <callable_iterator object at 0x7f0ffe7d30f0>

【今日の積み上げ】