Created
June 1, 2017 04:31
-
-
Save fuwac/daf7dfad3c677996adc3d75c4f46a7ef 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
# coding: UTF-8 | |
import os | |
import re | |
import sys | |
# マクロの情報 | |
class MacroInfo: | |
def __init__(self, name, value, comment): | |
self.name = name # マクロ名 | |
self.value = value # 値 | |
self.comment = comment # コメント | |
# マクロチェック | |
class MacroCheck: | |
# コンストラクタ的なやつ | |
def __init__(self): | |
self.list_macro = [] # マクロのリスト | |
# 実行 | |
def run(self, filepath): | |
text = "" | |
# マクロぶっこぬき用の正規表現 | |
macro_pattern = r"#define(\s|\t)+(?P<name>\w+)(\s|\t)+(?P<value>(-?)\w+)(\s|\t)+\/\/(?P<comment>.+)$" | |
# 抽出 | |
for line in open(filepath, "r"): | |
mc = re.search(macro_pattern, line) | |
if mc: | |
self.list_macro.append(MacroInfo(mc.group("name"), mc.group("value"), mc.group("comment"))) | |
# 表示 | |
for macro in self.list_macro: | |
print("{0} => {1} // {2}".format(macro.name, macro.value, macro.comment)) | |
# うごかす | |
param = sys.argv | |
m = MacroCheck() | |
m.run(param[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment