正则表达式可删除部分文本(在plist文件中)后的整个文本

问题描述 投票:0回答:1

我在这里浏览了各种各样的问题,但是找不到我的答案。我要删除跟随特定文本部分的整个文本。不在字符串内,而是整个下面的文本!

这里是一个plist的例子(实际上只是一个简单的例子。通常,plist会更长,但这与问题或答案无关):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>WFWorkflowActions</key>
    <array>
        <dict>
            <key>WFWorkflowActionIdentifier</key>
            <string>is.workflow.actions.comment</string>
            <key>WFWorkflowActionParameters</key>
            <dict>
                <key>WFCommentActionText</key>
                <string>Comment</string>
            </dict>
        </dict>
    </array>
    <key>WFWorkflowClientRelease</key>
    <string>3.0</string>
    <key>WFWorkflowClientVersion</key>
    <string>1030.14</string>
    <key>WFWorkflowIcon</key>
    <dict>
        <key>WFWorkflowIconGlyphNumber</key>
        <integer>59771</integer>
        <key>WFWorkflowIconStartColor</key>
        <integer>463140863</integer>
    </dict>
    <key>WFWorkflowImportQuestions</key>
    <array/>
    <key>WFWorkflowInputContentItemClasses</key>
    <array>
        <string>WFAppStoreAppContentItem</string>
        <string>WFArticleContentItem</string>
        <string>WFContactContentItem</string>
        <string>WFDateContentItem</string>
        <string>WFEmailAddressContentItem</string>
        <string>WFGenericFileContentItem</string>
        <string>WFImageContentItem</string>
        <string>WFiTunesProductContentItem</string>
        <string>WFLocationContentItem</string>
        <string>WFDCMapsLinkContentItem</string>
        <string>WFAVAssetContentItem</string>
        <string>WFPDFContentItem</string>
        <string>WFPhoneNumberContentItem</string>
        <string>WFRichTextContentItem</string>
        <string>WFSafariWebPageContentItem</string>
        <string>WFStringContentItem</string>
        <string>WFURLContentItem</string>
    </array>
    <key>WFWorkflowMinimumClientVersion</key>
    <integer>900</integer>
    <key>WFWorkflowMinimumClientVersionString</key>
    <string>900</string>
    <key>WFWorkflowTypes</key>
    <array>
        <string>NCWidget</string>
        <string>WatchKit</string>
    </array>
</dict>
</plist>

我想删除包括以下内容在内的所有内容:

</array>
    <key>WFWorkflowClientRelease</key>

必须保留所有换行符/换行符。

结果将如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>WFWorkflowActions</key>
    <array>
        <dict>
            <key>WFWorkflowActionIdentifier</key>
            <string>is.workflow.actions.comment</string>
            <key>WFWorkflowActionParameters</key>
            <dict>
                <key>WFCommentActionText</key>
                <string>Comment</string>
            </dict>
        </dict>

我什至找到了一种找到解决方案的方法,但是为此,我不得不删除所有新行,这是不希望的。我首先使用了\n。我将</array><key>WFWorkflowClientRelease</key>替换为lrtxplqw,然后删除了lrtxplqw之后的所有内容,包括lrtxplqw.*$。以这种非常尴尬的方式,我设法删除了所有内容,包括lrtxplqw及之后的内容。但是解决方案并不令人满意,因为必须保留换行符/新行。

我希望我能清楚,容易地表达自己,如果有人可以帮助我,我将非常高兴。

regex plist
1个回答
0
投票

您可以使用

^[\s\S]*?<array>|</array>\s*<key>WFWorkflowClientRelease</key>[\s\S]*

请参见regex demo

© www.soinside.com 2019 - 2024. All rights reserved.