Search code examples
regexreplacefindsentence

RegEx Find and Replace Sentence


I'm looking for a way to find and replace a sentence using regex. The regex should be able to find a sentence of any length. I can get the entire sentence with .* but that doesn't allow it to replace with \1.

FIND:
"QUESTION1" = "What is the day satellite called?"
"ANSWER1" = "The sun"

REPLACE:
<key>What is the day satellite called?</key>
<key>The sun</key>

Solution

  • You need to use capturing groups. So that you can refer the captured groups through back-reference.

    Regex:

    .*(?<= \")([^"]*).*
    

    Replacement string:

    <key>\1</key> 
    

    DEMO