简体   繁体   中英

How to fin and replace text between <wp:postmeta> </wp:postmeta> tags using Notepad++

I am using an XML export to remove old wanted data and replace it with relevant tags. I've been looking around but haven't been able to find exactly what i'm looking so forgive me if this seems redundant.

I am looking to replace all data within my <wp:postmeta> and </wp:postmeta> tags. However since they are across multiple lines the .*? are not working like they have in the past. I believe this means that its only across a single line. Here is a sample of the code:

<item>
    //Start of find

    <wp:postmeta>
        <wp:meta_key><![CDATA[_edit_last]]></wp:meta_key>
        <wp:meta_value><![CDATA[3]]></wp:meta_value>
    </wp:postmeta>
    <wp:postmeta>
        <wp:meta_key><![CDATA[_thumbnail_id]]></wp:meta_key>
        <wp:meta_value><![CDATA[51833]]></wp:meta_value>
    </wp:postmeta>
    <wp:postmeta>
        <wp:meta_key><![CDATA[snapEdIT]]></wp:meta_key>
        <wp:meta_value><![CDATA[1]]></wp:meta_value>
    </wp:postmeta>

    // End of find using Notepad++
</item>
<item>
    //Start of find

    <wp:postmeta>
        <wp:meta_key><![CDATA[_edit_last]]></wp:meta_key>
        <wp:meta_value><![CDATA[3]]></wp:meta_value>
    </wp:postmeta>
    <wp:postmeta>
        <wp:meta_key><![CDATA[_thumbnail_id]]></wp:meta_key>
        <wp:meta_value><![CDATA[51425]]></wp:meta_value>
    </wp:postmeta>

    // End of find using Notepad++
</item>

I am specifically looking to find and replace everything from the first <wp:postmeta> tag to the end of the last closing </wp:postmeta> within its corresponding open and closing item tags. There are hundreds of these and I would prefer not to go one by one. Please let me know if this is possible. If it is not possible I would like to know how to go from one <wp:postmeta> to its closing </wp:postmeta> and I could work with that.

Thanks in advance.

In Notepad++, to force a . to match a newline (a multiline text block), you need to enable the . matches newline . matches newline option in the Find and Replace dialog.

Note that making changes in a (X)HTML code with regex is very difficult and error prone.

So, if you know what you are doing, you can enable that option I mentioned above and use the regex <wp:postmeta>.*?</wp:postmeta> and replace with whatever you need. See regex demo .

If there are large text blocks without the closing </wp:postmeta> , you may run into a timeout issue. Then, you will need to unroll the regex:

<wp:postmeta>[^<]*(?:<(?!/wp:postmeta>)[^<]*)*</wp:postmeta>

See regex demo. This regex will match newlines regardless of the . matches newline . matches newline option.

If you need to match adjacent wp:postmeta tags, you can use a non-capturing group around the pattern and set a + quantifier to it:

(?:\s*<wp:postmeta>[^<]*(?:<(?!/wp:postmeta>)[^<]*)*</wp:postmeta>)+

See one more demo

A screenshot:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM