Python - split()関数でパターンを検出する

次の例は、Python の split 関数を使用してパターンを検出し、文字列を分割する方法を示しています。関数 split() は、文字列内のすべての単語のリストを結果として返します。

スペースで分割

関数 split() はデフォルトで空またはスペース文字を区切り文字として使用します.

alphabet = "a b c d e f"
tstr = alphabet.split()

for c in tstr:
print c
Runtime:

a
b
c
d
e
f
関数 split() は、以下の例に示すように、文字またはパターンの最大検出数もパラメーターとして取ります:

alphabet = "a b c d e f"
tstr = alphabet.split(" ", 3)

tstr の c の場合:
print c
Runtime:

a
b
c
d e f

例2:ドメイン名の抽出

s = 'exemple.domaine.net'
i = s.split('.', 1)
nom_de_domaine = i[1]
print nom_de_domaine
Runtime:

domaine.net
References:
Python String split() メソッド