Last active
May 10, 2016 17:54
-
-
Save nwiizo/a27400bbc2bed49e160faa6f1dc45419 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
match = re.compile(r'\[\[Category\:(((?P<m2>.*?)\|+.*)|(?P<m1>.*?))\]\]') | |
#?は直前にある正規表現要素を0回か1回繰り替えした表現です | |
#.は改行文字 '\n' 以外の任意の単一文字 | |
#*はその左側のパターンの 0回以上の出現 | |
#(?P name)はブログに載せています。 | |
#文字列の後に|がある場合をm1に代入される | |
with open("data02","rt") as f: | |
for l in f: | |
if match.match(l): | |
match01 = match.search(l) | |
if match01.group("m1"): | |
#m1が存在する場合は m1を出力する | |
print(match01.group("m1")) | |
elif match01.group("m2"): | |
#m2が存在する場合はm2を出力する | |
print(match01.group("m2")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment