Skip to content

Instantly share code, notes, and snippets.

@nwiizo
Last active May 10, 2016 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nwiizo/a27400bbc2bed49e160faa6f1dc45419 to your computer and use it in GitHub Desktop.
Save nwiizo/a27400bbc2bed49e160faa6f1dc45419 to your computer and use it in GitHub Desktop.
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