连接2个字符串的不常见文字,在python

时间:2016-04-07 06:07:29

标签: python python-2.7

我有两个字符串,如下图所示,

var_1=('class="input {{#if Required}}required{{/if}}"')
var_2=('class="input {{#if emailRequired}}required{{/if}}{{#if contactPointRequired}}required{{/if}}"')

我的预期结果是

'class="input {{#if Required}}{{#if emailRequired}}required{{/if}}{{#if contactPointRequired}}required{{/if}}"'.

我的意思是说,我希望新字符串在两个字符串中都有共同的元素以及两个字符串的区别。

我如何在python 2.7中实现这一点?

1 个答案:

答案 0 :(得分:0)

您可以采用以下方法:

  1. (可选)将字符串拆分为有意义的标记

  2. 为字符串或代币列表创建difflib.SequenceMatcher

  3. 根据需要格式化输出。

  4. 原油示例:

    s1 = 'class="input {{#if Required}}required{{/if}}"'
    s2 = 'class="input {{#if emailRequired}}required{{/if}}{{#if contactPointRequired}}required{{/if}}"'
    
    # 0. Split strings into meaningful parts
    # In this case I am using a crude regex to detect token boundaries
    # Remember, this step is optional
    
    import re
    pat = re.compile(r'(\=+|\"+| |\{+|\}+)')
    a = pat.split(s1)
    b = pat.split(s2)
    
    # 1. Create SequenceMatcher
    
    from difflib import SequenceMatcher
    matcher = SequenceMatcher(a=a, b=b, autojunk=False)
    
    # 2. Format output
    # Here I am using ANSI escape codes to colorize the output
    
    GREEN='\033[92m'
    RED='\033[91m'
    ENDC='\033[0m'
    line = []
    for op, ai, aj, bi, bj in matcher.get_opcodes():
        if op == 'equal':
            line.extend(a[ai:aj])
        else:
            if aj > ai:
                line.append(RED)
                line.extend(a[ai:aj])
                line.append(ENDC)
            if bj > bi:
                line.append(GREEN)
                line.extend(b[bi:bj])
                line.append(ENDC)
    
    print(''.join(line))