Python string pattern recognition/compression

前端 未结 6 907
隐瞒了意图╮
隐瞒了意图╮ 2021-02-15 14:45

I can do basic regex alright, but this is slightly different, namely I don\'t know what the pattern is going to be.

For example, I have a list of similar strings:

<
6条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-15 15:35

    Here is a scary one to get the ball rolling.

    >>> import re
    >>> makere = lambda n: ''.join(['(.*?)(.+)(.*?)(.+)(.*?)'] + ['(.*)(\\2)(.*)(\\4)(.*)'] * (n - 1))
    >>> inp = ['asometxt0moretxt', 'bsometxt1moretxt', 'aasometxt10moretxt', 'zzsometxt999moretxt']
    >>> re.match(makere(len(inp)), ''.join(inp)).groups()
    ('a', 'sometxt', '0', 'moretxt', '', 'b', 'sometxt', '1', 'moretxt', 'aa', '', 'sometxt', '10', 'moretxt', 'zz', '', 'sometxt', '999', 'moretxt', '')
    

    I hope its sheer ugliness will inspire better solutions :)

提交回复
热议问题