【问题标题】:Python regular expression matching a multiline block of text but not replacing itPython正则表达式匹配多行文本块但不替换它
【发布时间】:2009-11-26 13:38:31
【问题描述】:

好的,我有这段代码:

def findNReplaceRegExp(file_name, regexp, replaceString, verbose=True, confirmationNeeded=True):
'''Replaces the oldString with the replaceString in the file given,\
   returns the number of replaces
'''
    # initialize local variables
    cregexp = re.compile(regexp, re.MULTILINE | re.DOTALL)
    somethingReplaced = True
    ocurrences = 0
    isAborted = False

    # open file for read
    file_in = open(file_name, 'r')
    file_in_string = file_in.read()
    file_in.close()

    while somethingReplaced:
        somethingReplaced = False
        # if the regexp is found
        if cregexp.search(file_in_string):
            # make the substitution
            replaced_text = re.sub(regexp, replaceString, file_in_string)
            if verbose == True:
                # calculate the segment of text in which the resolution will be done

                # print the old string and the new string
                print '- ' + file_in_string
                print '+ ' + replaced_text
                if confirmationNeeded:
                    # ask user if this should be done
                    question = raw_input('Accept changes? [Yes (Y), No (n), Abort (a)] ')
                    question = string.lower(question)
                    if question == 'a':
                        isAborted = True
                        print "Aborted"
                        break
                    elif question == 'n':
                        pass
                    else:
                        file_in_string = replaced_text
                        somethingReplaced = True
                        ocurrences = ocurrences + 1
            else:
                file_in_string = replaced_text
                somethingReplaced = True
                ocurrences = ocurrences + 1

    # if some text was replaced, overwrite the original file
    if ocurrences > 0 and not isAborted:
        # open the file for overwritting
        file_out = open(file_name, 'w')
        file_out.write(file_in_string)
        file_out.close()
        if verbose: print "File " + file_name + " written"

还有这个文件

CMC_SRS T10-24400: DKU Data Supply: SN Time Break-In Area
CMC_SRS T10-24401: DKU Data Supply: SN Transponder Enable Area
CMC_SRS T10-24402: DKU Data Supply: SN Adjust Master Slave Area
CMC_SRS T10-24403: DKU Data Supply: SN ATEC Area
CMC_SRS T10-24404: DKU Data Supply: SN PTEC Area
CMC_SRS T10-25449: DKU Data Supply: SN Self Init Area
CMC_SRS T10-24545: DKU Data Supply: SN Time Area
CMC_SRS T10-4017: RFI display update
CMC_SRS T10-6711: Radio Interface to PLS Equipment
CMC_SRS T10-21077: Safety Requirements: Limit FM Power

当我用这个文件和这些参数调用程序时: 正则表达式=24403.*24404 替换=TESTSTRING

我遇到了一个巧合(它匹配并质疑要做什么)但是当它该替换的时候什么都没有发生......怎么了??

【问题讨论】:

    标签: python regex replace multiline


    【解决方案1】:

    您使用cregexp 查找,它设置了多行选项,但随后替换为regexp,这可能会也可能不会。

    【讨论】:

    • 您可以将regexp 替换为cregexp,因为re.sub 接受字符串和正则表达式对象作为其第一个参数。
    猜你喜欢
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 2011-10-11
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    相关资源
    最近更新 更多