。The thing is that any phrase can be interrupted with some XML tags, that can come between words, or even inside words, as you can see in the example:
</w:rPr><w:t> To i</w:t></w:r><w:r wsp:rsidRPr="00EC3076"><w:rPr><w:sz w:val="17"/><w:lang w:fareast="JA"/></w:rPr><w:t>ncrease knowledge of and acquired skills for implementing social policies with a view to strengthening the capacity of developing countries at the national and community level.</w:t></w:r></w:p>
因此,我处理这个问题的方法是简单地将所有XML标记缩减为长度相同的字符簇,这样当我可以找到任何短语时,regex将忽略每两个字符之间的所有XML标记。
我基本上需要的是这个短语在实际的xml文档中的跨度,所以我将在以后处理xml文档时使用这个跨度,我不能使用克隆。

================================
下面是一个例子:
I have this text where there are some clusters of # characters within it (which I want to keep), and the spaces are also unpredictable, such as the following:
与“关系”的关系355;ţţţţţţ355;355;ţ355;ţţţţţţţţţţţţţţţţţţţţţţţţţţţţ355;ţţ355;;ţ355;\355;;35预期的
#######
为了匹配以下短语:
与2014-2015年期间战略框架的关系:
programme 7, Economic and Social Affairs, subprogramme 3, expected
accomplishment (c)
我想出了这个正则表达式来适应不可预测的字符和空格:
u'R#*e#*l#*a#*t#*i#*o#*n#*s#*h#*i#*p#*\\s*#*t#*o#*\\s*#*t#*h#*e#*\\s*#*s#*t#*r#*a#*t#*e#*g#*i#*c#*\\s*#*f#*r#*a#*m#*e#*w#*o#*r#*k#*\\s*#*f#*o#*r#*\\s*#*t#*h#*e#*\\s*#*p#*e#*r#*i#*o#*d#*\\s*#*2#*0#*1#*4#*\\-#*2#*0#*1#*5#*:#*\\s*#*p#*r#*o#*g#*r#*a#*m#*m#*e#*\\s*#*7#*\\,#*\\s*#*E#*c#*o#*n#*o#*m#*i#*c#*\\s*#*a#*n#*d#*\\s*#*S#*o#*c#*i#*a#*l#*\\s*#*A#*f#*f#*a#*i#*r#*s#*\\,#*\\s*#*s#*u#*b#*p#*r#*o#*g#*r#*a#*m#*m#*e#*\\s*#*3#*\\,#*\\s*#*e#*x#*p#*e#*c#*t#*e#*d#*\\s*#*a#*c#*c#*o#*m#*p#*l#*i#*s#*h#*m#*e#*n#*t#*\\s*#*\\(#*c#*\\)'
它在我想匹配的所有其他短语中都工作得很好,但是这个短语有一个问题导致了一些灾难性的回溯,有人能发现它吗?
原始文本用xml标记分隔,因此为了使regex更简单,我用这些集群替换了标记,下面是原始文本:
</w:rPr><w:t>Relationship to the </w:t></w:r><w:r><w:rPr><w:i/><w:sz w:val="17"/><w:sz-cs w:val="17"/></w:rPr><w:t>strategic framework </w:t></w:r><w:r wsp:rsidRPr="00EC3076"><w:rPr><w:i/><w:sz w:val="17"/><w:sz-cs w:val="17"/></w:rPr><w:t> for the period 2014-2015</w:t></w:r><w:r wsp:rsidRPr="00EC3076"><w:rPr><w:sz w:val="17"/><w:sz-cs w:val="17"/></w:rPr><w:t>: Programme 7, Economic and Social Affairs, subprogramme 3, expected accomplishment (c)</w:t>

最佳答案

Since the situation is that complex - don't use regex, just go through your line symbol by symbol:

etalone = "String to find"
etalone_length = len(etalone)
counter = 0
for symbol in your_line:
    if symbol == etalone[counter]:
        counter += 1
        if counter == etalone_length:
            print("String matches")
            break
    elif symbol != " " and sybmol != "#":
        # Bad char found
        print("Does not match!")
else:  # exited 'for' before full etalone matched
    print("Does not match!")


How about this instead:
克隆字符串
从克隆中删除“35;”
与模式匹配
If pattern matches - get the location of matched result
根据这个位置-找到第一个符号的确切匹配项。例如,如果整行是a#b##ca#d#f并且我们要查找的行是adf,那么我们将从第二个a符号开始匹配。
。设置计数器=
使用上述算法(存储为span start,在a之前存储为span end)

09-27 08:15