javaregexboundary

Need to replace <p>Are you? [xxxrecipientFirstNamexxx] </p> with <p>Are you? {recipientFirstName} </p>


Need to replace

Are you? [xxxrecipientFirstNamexxx]

with

Are you? {recipientFirstName}

. I tried using Boundary Matchers .But result is not as expected . I tried with below code

"<p>Are you? [xxxrecipientFirstNamexxx] </p>".replaceAll("\\b[[xxxrecipientFirstNamexxx]]\\b", "{recipientFirstName}");

.Can any one help on this?


Solution

  • Use REGEX to find and replace:

    Find: \[xxx(.*)xxx\]

    Replace it with {$1}

    In python:

    import re
    
    line = re.sub(
               r"\[xxx(.*)xxx\]", 
               "{$1}", 
               line
           )