regextextwrangler

Regular Expression, only replace first occurrence of HTML tag


I've got several files that have double <body> tags in them (either on purpose or by accident). I'm looking to find the first occurrence only of the <body> tag and append it with additional HTML code. But the second occurrence shouldn't be affected. I'm using TextWrangler. The regex I'm using now replaces both occurrences rather than just the first.

Text:

<body someattribute=...>
existing content
<body onUnload=...>

RegEx I'm using:

Find: (\<body.*\>)

Replace with: 

\n\1
appended HTML code

Current result:

<body someattribute=...>
appended HTML code
existing content
<body onUnload=...>
appended HTML code

So it's adding my appended code twice. I just want it to happen to the first <body...> only.


Solution

  • Regex:

    (?s)(<body.*?>)(.*)
    

    Replace:

    \1\nappended content\n\2
    

    Explanation:

    Tested in Notepad++