• Login
Community
  • Login

Replace html tag with regular expression

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
htmlxmlnewbie
2 Posts 2 Posters 829 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L
    Linh Việt
    last edited by Mar 8, 2023, 2:42 PM

    Hello everyone. I have this html code:

    <hint><type="book"><string-name><surname>Alex</surname>, <given-names>Nere</given-names></string-name>, <year>2012</year>, <article-title>The Dark Side of Money</article-title>, <source>Priest With A Goal</source></type></hint>
    <hint><type="journal"><string-name><surname>Radosław</surname>, <given-names>Natali</given-names></string-name>, <year>2013</year>, <article-title>8 Mood-Boosting Benefits of Money</article-title>, <source>Phantom Without A Conscience</source>, <volume>1</volume></type></hint>
    <hint><type="journal"><string-name><surname>Eamon</surname>, <given-names>Heino</given-names></string-name>, <year>2014</year>, <article-title>How Millennials Are Disrupting Money</article-title>, <source>Humans Of Power</source>, <volume>II</volume></type></hint>
    <hint><type="other"><string-name><surname>Marta</surname>, <given-names>Amice</given-names></string-name>, <year>2015</year>, <article-title>Here's My Secret Sauce for Success in Money</article-title>, <source>Enemies Of The Nation</source>, <issue>2</issue></type></hint>
    <hint><type="other"><string-name><surname>Vepkhia</surname>, <given-names>Terrie</given-names></string-name>, <year>2016</year>, <article-title>Why the Money Business Is Flirting With Disaster</article-title>, <source>Lords And Priests</source></type></hint>
    <hint><type="book"><string-name><surname>Ulf</surname>, <given-names>Nima</given-names></string-name>, <year>2017</year>, <article-title>13 Hilarious Tweets About Money</article-title>, <source>Aliens And Turtles</source></type></hint>
    <hint><type="other"><string-name><surname>Baard</surname>, <given-names>Irine</given-names></string-name>, <year>2018</year>, <article-title>Forget Everything You’ve Ever Known About Money</article-title>, <source>Faction Without Courage</source></type></hint>
    <hint><type="other"><string-name><surname>Kalyani</surname>, <given-names>Loreta</given-names></string-name>, <year>2019</year>, <article-title>This Will Fundamentally Change the Way You Look at Money</article-title>, <source>Construction Of Joy</source></type></hint>
    <hint><type="journal"><string-name><surname>Hubertus</surname>, <given-names>Camilla</given-names></string-name>, <year>2020</year>, <article-title>The 15 Best War Sites on the Internet</article-title>, <source>Never Trust The North</source>, <volume>I</volume>, <issue>1</issue></type></hint>
    

    I want replace all <article-title> to <chapter-title> where <type=“book”>
    Can anyone give me a solution to this problem because the line in my file is a lot
    Thanks for reading and giving me the solution

    Best regards!

    P 1 Reply Last reply Mar 8, 2023, 2:53 PM Reply Quote 1
    • P
      PeterJones @Linh Việt
      last edited by PeterJones Mar 8, 2023, 3:13 PM Mar 8, 2023, 2:53 PM

      @Linh-Việt ,

      So, you want to replace any <article-title> with <chapter-title>, but only when inside a <type="book"> .... </type> pair, and not between other types, like <type="journal">...</type>?

      That sounds like a job for FAQs > Generic Regex > Replacing in a specific zone of text , where you could use:

      • Find Regex FR = article-title>
      • Replacement Regex RR = chapter-title>
      • Begin Search Region BSR = <type="book">
      • End Search Region ESR = </type>

      Using those values in the bold “variables” in the regex shown in that article, you would get:

      • Find What = (?-si:<type="book">|(?!\A)\G)(?s-i:(?!</type>).)*?\K(?-si:article-title>)
      • Replace With = chapter-title>
      • Replace All will convert the text for you.
        • (doing multiple Replace commands won’t necessarily work, because of the way that \K works)

      BTW: thanks for formatting your example data correctly. It helps us make sure we read your data correctly!

      ----

      Useful References

      • Notepad++ Online User Manual: Searching/Regex
      • FAQ: Where to find other regular expressions (regex) documentation
      1 Reply Last reply Reply Quote 1
      1 out of 2
      • First post
        1/2
        Last post
      The Community of users of the Notepad++ text editor.
      Powered by NodeBB | Contributors