1
4

1 に答える 1

1

この正規表現は、探しているケースを処理する必要があります (最初の列の可能な限り長いパターンの一致を含む):

^(\S+)(\S*?)\s+?(\S*?(\1)\S*?)$

正規表現のデモはこちら.

次に、一致グループを使用して、探している特定の置換を作成できます。Python でのソリューションの例を次に示します。

import re

regex = re.compile(r'^(\S+)(\S*?)\s+?(\S*?(\1)\S*?)$')

with open('output.txt', 'w', encoding='utf-8') as fout:
    with open('file.txt', 'r', encoding='utf-8') as fin:
        for line in fin:
            match = regex.match(line)
            if match:
                hint = match.group(3).replace(match.group(1), '{...}')
                output = '{0}\t{1}\n'.format(match.group(1) + match.group(2), hint)
                fout.write(output)

Python のデモはこちら

于 2015-07-23T12:07:14.527 に答える