[Python] How can I use regex to remove XML tags between </ref-list> and the next <ref-list>?...

Discussão em 'Python' iniciado por Stack, Fevereiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    Remove the XML tags from the list. Identify the first occurrence where the list item ends with , and then locate the next occurrence of the tag. Replace everything between and the next with an empty string.

    I need help with removing specific sections from an list XML file using regular expression. For this task , I have provided an example XML input and desired output. please help me regex pattern that will identity and replace these sections with empty spaces in the xml input to match the desire

    Listed XML

    ['```xml
    <?xml version="1.0" encoding="UTF-8"?>
    <article>
    <ref-list>
    <ref id="ref1">
    <label>1</label>
    <mixed-citation publication-type="journal">
    <string-name>
    <surname>Sasdf</surname>
    <given-names>c.k.</given-names>
    </string-name>,
    <string-name>
    <surname>ssdsd</surname>
    <given-names>a.a.</given-names>
    </string-name>
    (<ear>000</ear>).
    <t-a>.smdf.cm</t-a>.
    <source>z/sd.vmm/dsmkV</source>.
    <m>sdnlk. 00</m>,
    <tissue>no. 1</tissue>,
    <gage>kk. 00</gage>-
    <age>00</age>,
    doi: <object-id>10.00000/0000-0000-000-00-1-00-00</object-id>
    <comment>(000., 000 00 000.)</comment>.
    </mixed-citation>
    </ref>

    </ref-list>
    </article>
    ```', '```xml
    <?xml version="1.0" encoding="UTF-8"?>
    <article>
    <ref-list>
    <ref id="ref16">
    <cabel>00</cabel>
    <c-m t-p="cccc">
    <n-s>
    <mm>ddxxxdd</mm>
    <f-f>X.X.</f-f>
    </n-s>,
    <n-s>
    <mm>Dffff</mm>
    <g-s>,X.X.</g-s>
    </n-s>,
    kol. <volume>0</volume>.
    <publisher-loc>cdffgf</publisher-loc>:
    <publisher-name>Sdfr’</publisher-name>.
    <page-count count="000"/>
    0<isbn>000 -0-0000-00-3</isbn>.
    <ct>(jj sss)</ct>.
    </mixed-citation>
    </ref>
    </ref-list>
    </article>
    ```', '```xml
    <?xml version="1.0" encoding="UTF-8"?>
    <article>
    <back>
    <ref-list>
    <ref id="xyz00">
    <label>00</label>
    <c-m y-x="OOOO">
    </c-m>
    </ref>

    <ref id="ref00">
    <label>00</label>
    <c-m t-p="fffff">
    <t-s>
    <d>ssss</d>
    <g-b>1.</g-b>
    </t-s>
    <lear>(<lear>2010</lear>)</lear>
    <g>ffffff ddddd d aaaaa</g>
    <ct>= cfffff dddd vvv aaaaa</ct>.
    <tissue>No. 0</tissue>,
    <cage>pp. 00-00</cage>.
    <ext-link>cable ss: https://www.dddddd.aa/ssdffsd/dfff_ss_dsc.xyz</ext-link>
    <date>(00000: <lday>00</lday>.<sumnath>00</sumnath>.<ear>0000</ear>)</date>
    <ts>(dd Ssssts ddddddd aa aaa.)</ts>.
    </c-m>
    </ref>
    </ref-list>
    </back>
    </article>
    ```','```xml
    <ref-list>
    <ref id="ref1">
    <label>1</label>
    <c-m t-p="abc">
    <string-name>

    <given-names>XXX</given-names>
    </string-name>.
    <ABC-XYZ>aegf asclcsnlac</ABC-XYZ>.
    <source>sed sdv asedf</source>.
    <NO>0000</NO>;
    </c-m>
    </ref>
    </ref-list>
    ```', '```xml
    <?xml version="1.0" encoding="UTF-8"?>
    <article>
    <back>
    <ref-list>
    <ref id="ref16">
    <label>16</label>
    <c-m t-p="abc">
    <string-name>
    <surname>ABC</surname>
    <given-names>S</given-names>
    </string-name>,


    <at>ab cd</at>.
    <ABC-XYZ>XYZ</ABC-XYZ>
    <source>ABC</source>.
    <sd>15</sd>:
    <NO>0000</year>;
    <cage>000<cage>–
    <kage>000</kage>.
    </c-m>
    </ref>
    <ref id="ref26">
    <label>00</label>
    <mixed-citation publication-type="journal">
    <string-name>
    <surname>sedv</surname>
    <given-names>sedv</given-names>
    </string-name>,
    <etal>et al</etal>.
    <article-title>sdvsdv</article-title>
    <source>sdv</source>.
    <cage>000</cage>–
    <page>000</page>.
    </mixed-citation>
    </ref>
    </ref-list>
    </back>
    </article>
    ```]


    Desired Output:

    ['```xml
    <?xml version="1.0" encoding="UTF-8"?>
    <article>
    <ref-list>
    <ref id="ref1">
    <label>1</label>
    <mixed-citation publication-type="journal">
    <string-name>
    <surname>Sasdf</surname>
    <given-names>c.k.</given-names>
    </string-name>,
    <string-name>
    <surname>ssdsd</surname>
    <given-names>a.a.</given-names>
    </string-name>
    (<ear>000</ear>).
    <t-a>.smdf.cm</t-a>.
    <source>z/sd.vmm/dsmkV</source>.
    <m>sdnlk. 00</m>,
    <tissue>no. 1</tissue>,
    <gage>kk. 00</gage>-
    <age>00</age>,
    doi: <object-id>10.00000/0000-0000-000-00-1-00-00</object-id>
    <comment>(000., 000 00 000.)</comment>.
    </mixed-citation>
    </ref>

    <ref id="ref16">
    <cabel>00</cabel>
    <c-m t-p="cccc">
    <n-s>
    <mm>ddxxxdd</mm>
    <f-f>X.X.</f-f>
    </n-s>,
    <n-s>
    <mm>Dffff</mm>
    <g-s>,X.X.</g-s>
    </n-s>,
    kol. <volume>0</volume>.
    <publisher-loc>cdffgf</publisher-loc>:
    <publisher-name>Sdfr’</publisher-name>.
    <page-count count="000"/>
    0<isbn>000 -0-0000-00-3</isbn>.
    <ct>(jj sss)</ct>.
    </mixed-citation>
    </ref>
    <ref id="xyz00">
    <label>00</label>
    <c-m y-x="OOOO">
    </c-m>
    </ref>

    <ref id="ref00">
    <label>00</label>
    <c-m t-p="fffff">
    <t-s>
    <d>ssss</d>
    <g-b>1.</g-b>
    </t-s>
    <lear>(<lear>2010</lear>)</lear>
    <g>ffffff ddddd d aaaaa</g>
    <ct>= cfffff dddd vvv aaaaa</ct>.
    <tissue>No. 0</tissue>,
    <cage>pp. 00-00</cage>.
    <ext-link>cable ss: https://www.dddddd.aa/ssdffsd/dfff_ss_dsc.xyz</ext-link>
    <date>(00000: <lday>00</lday>.<sumnath>00</sumnath>.<ear>0000</ear>)</date>
    <ts>(dd Ssssts ddddddd aa aaa.)</ts>.
    </c-m>
    </ref>

    <ref id="ref1">
    <label>1</label>
    <c-m t-p="abc">
    <string-name>

    <given-names>XXX</given-names>
    </string-name>.
    <ABC-XYZ>aegf asclcsnlac</ABC-XYZ>.
    <source>sed sdv asedf</source>.
    <NO>0000</NO>;
    </c-m>
    </ref>
    <ref id="ref16">
    <label>16</label>
    <c-m t-p="abc">
    <string-name>
    <surname>ABC</surname>
    <given-names>S</given-names>
    </string-name>,


    <at>ab cd</at>.
    <ABC-XYZ>XYZ</ABC-XYZ>
    <source>ABC</source>.
    <sd>15</sd>:
    <NO>0000</year>;
    <cage>000<cage>–
    <kage>000</kage>.
    </c-m>
    </ref>
    <ref id="ref26">
    <label>00</label>
    <mixed-citation publication-type="journal">
    <string-name>
    <surname>sedv</surname>
    <given-names>sedv</given-names>
    </string-name>,
    <etal>et al</etal>.
    <article-title>sdvsdv</article-title>
    <source>sdv</source>.
    <cage>000</cage>–
    <page>000</page>.
    </mixed-citation>
    </ref>
    </ref-list>
    </back>
    </article>
    ```]


    Removed the XML tags from the list: the list of the first item ends and the next item first tag select from that to replace with empty

    </ref-list>
    </article>
    ```', '```xml
    <?xml version="1.0" encoding="UTF-8"?>
    <article>
    <ref-list>



    </ref-list>
    </article>
    ```', '```xml
    <?xml version="1.0" encoding="UTF-8"?>
    <article>
    <back>
    <ref-list>


    </ref-list>
    </back>
    </article>
    ```','```xml
    <ref-list>


    /ref-list>
    ```', '```xml
    <?xml version="1.0" encoding="UTF-8"?>
    <article>
    <back>
    <ref-list>

    Continue reading...

Compartilhe esta Página