Replace html tag with regular expression
-
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 solutionBest regards!
-
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)
- (doing multiple Replace commands won’t necessarily work, because of the way that
BTW: thanks for formatting your example data correctly. It helps us make sure we read your data correctly!
----
Useful References
- Find Regex FR =