0

私の python スクリプトは、ファイル内の各行を読み取り、各行で多くの正規表現の置換を行います。

正規表現が成功した場合は、次の行にスキップします

この種のスクリプトを高速化する方法はありますか?
代わりに subn を呼び出し、置換が完了したかどうかを確認してから、残りのものにスキップする価値はありますか?
正規表現をコンパイルすると、コンパイルされたすべての正規表現をメモリに保存できますか?

for file in files:  
     for line in file:  
         re.sub() # <--- ~ 100 re.sub

PS: 各正規表現の代替 vaires

4

2 に答える 2

2

@Tim Pietzcker が言ったように、代替にすることで正規表現の数を減らすことができます。一致オブジェクトの「lastindex」属性を使用して、どの代替が一致したかを判断できます。

これはあなたができることの例です:

>>> import re
>>> replacements = {1: "<UPPERCASE LETTERS>", 2: "<lowercase letters>", 3: "<Digits>"}
>>> def replace(m):
...     return replacements[m.lastindex]
...
>>> re.sub(r"([A-Z]+)|([a-z]+)|([0-9]+)", replace, "ABC def 789")
'<UPPERCASE LETTERS> <lowercase letters> <Digits>'
于 2012-08-26T01:26:15.507 に答える
2

You should probably do three things:

  1. Reduce the number of regexes. Depending on differences in the substitution part, you might be able to combine them all into a single one. Using careful alternation, you can determine the sequence in which parts of the regex will be matched.
  2. If possible (depending on file size), read the file into memory completely.
  3. Compile your regex (only for readability; it won't matter in terms of speed as long as the number of regexes stays below 100).

This gives you something like:

regex = re.compile(r"My big honking regex")
for datafile in files:
    content = datafile.read()
    result = regex.sub("Replacement", content)
于 2012-08-25T05:53:52.827 に答える