纯正则表达式解决方案,用于删除特定部分文本后的整个文本(在 plist 文件中)
Pure regex solution to remove entire text after a certain part of text (in a plist file)
我在这里查看了各种问题,但找不到我的答案。我想删除特定文本部分之后的整个文本。不是在一个字符串中,而是在整个以下文本中!
这里是一个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>
必须保留所有第 breaks/new 行。
结果将如下所示:
<?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
。我用 lrtxplqw
替换了 </array><key>WFWorkflowClientRelease</key>
,然后用 lrtxplqw.*$
删除了 lrtxplqw
之后的所有内容,包括 lrtxplqw
。以这种非常尴尬的方式,我设法删除了所有内容,包括 lrtxplqw
和之后的内容。但是解决方案并不理想,因为第breaks/new行全部要保留
我也很乐意删除第一部分,即:
<?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>
您可以使用
^[\s\S]*?<array>|</array>\s*<key>WFWorkflowClientRelease</key>[\s\S]*
参见regex demo。
<?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>buildVersion</key>
<string>0.9064</string>
<key>comments</key>
<string></string>
<key>data</key>
<string><levelXML><info v="1.70" x="75.00" y="50.00" c="2" f="0" h="0" bg="0" bgc="16777215" e="1" fm="m"/></levelXML> </string>
<key>force_character</key>
<false/>
<key>name</key>
<string>Untitled</string>
<key>playable_character</key>
<integer>2</integer>
</dict>
</plist>
我在这里查看了各种问题,但找不到我的答案。我想删除特定文本部分之后的整个文本。不是在一个字符串中,而是在整个以下文本中!
这里是一个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>
必须保留所有第 breaks/new 行。
结果将如下所示:
<?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
。我用 lrtxplqw
替换了 </array><key>WFWorkflowClientRelease</key>
,然后用 lrtxplqw.*$
删除了 lrtxplqw
之后的所有内容,包括 lrtxplqw
。以这种非常尴尬的方式,我设法删除了所有内容,包括 lrtxplqw
和之后的内容。但是解决方案并不理想,因为第breaks/new行全部要保留
我也很乐意删除第一部分,即:
<?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>
您可以使用
^[\s\S]*?<array>|</array>\s*<key>WFWorkflowClientRelease</key>[\s\S]*
参见regex demo。
<?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>buildVersion</key>
<string>0.9064</string>
<key>comments</key>
<string></string>
<key>data</key>
<string><levelXML><info v="1.70" x="75.00" y="50.00" c="2" f="0" h="0" bg="0" bgc="16777215" e="1" fm="m"/></levelXML> </string>
<key>force_character</key>
<false/>
<key>name</key>
<string>Untitled</string>
<key>playable_character</key>
<integer>2</integer>
</dict>
</plist>