'Pure regex solution to remove entire text after a certain part of text (in a plist file)
I've looked through all sorts of questions here, but couldn't find an answer to mine. I want to remove the entire text that follows a particular text part. Not within a string, but really the whole following text!
Here’s an example of a plist (it's really just a simple example. Normally the plist is much longer, but this should have no relevance for the question or answer):
<?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>
I want to remove everything including and after:
</array>
<key>WFWorkflowClientRelease</key>
All line breaks/new lines must be kept.
The result would then look like this:
<?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>
I even found a way to find a solution, but for that I had to remove all new lines, which is not desired. I first used \n
. Than I replaced </array><key>WFWorkflowClientRelease</key>
with lrtxplqw
and then removed everything after and including lrtxplqw
with lrtxplqw.*$
. In this very awkward way I managed to have everything removed including and after lrtxplqw
. But the solution is not satisfactory, because the line breaks/new lines all have to be kept.
I would also be happy to remove the first part, which would be:
<?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>
The final result would look like this:
<dict>
<key>WFWorkflowActionIdentifier</key>
<string>is.workflow.actions.comment</string>
<key>WFWorkflowActionParameters</key>
<dict>
<key>WFCommentActionText</key>
<string>Comment</string>
</dict>
</dict>
Solution 1:[1]
You may use
^[\s\S]*?<array>|</array>\s*<key>WFWorkflowClientRelease</key>[\s\S]*
See the regex demo.
Solution 2:[2]
<?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>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Bob |