Skip to content

Instantly share code, notes, and snippets.

@otknoy
Created April 9, 2014 11:16
Show Gist options
  • Save otknoy/10256402 to your computer and use it in GitHub Desktop.
Save otknoy/10256402 to your computer and use it in GitHub Desktop.
Python で正規表現を用いて日付表現を抽出する
#!/usr/bin/env python
import re
def extract_date(s):
date_pattern = re.compile('(\d{4})/(\d{1,2})/(\d{1,2})')
result = date_pattern.search(s)
if result:
y, m, d = result.groups()
return {'year': y, 'month': m, 'day': d}
else:
return None
if __name__ == '__main__':
import sys
filename = sys.argv[1]
f = open(filename)
text = f.read()
f.close()
for line in text.splitlines():
result = extract_date(line)
if result:
print result
print line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment