Search code examples
javahtmlregexexpressionjakarta-mail

Can not replace a certain text in an html string with java


I have a method that suppose to replace part of an html string before sending it to clients email. I have tried using java replaceAll() method but didnt succeed. Here is what i tried so far:

data.replaceAll("dd%title%dd", "This is the Title");
data.replaceAll("dd%message%dd", "This is the message body");

but when i tried this it doesnt work and i keep getting the string without it been replaced. Here is the image of the message i recieved in yahoo mail inbox: Yahoo mail screenshot

I have tried using regex expression to replace but its not working as i expected.

Here is what i have now

/******************* CONSTRUCTING THE MESSAGE TRANSLATOR ********************/
    private String msgTranslate(String subject, String messaging){
        // HERE WE START CONSTRUCTING THE MESSAGE TRANSLATE
        String content="";
        String data="";
        DjadeUtil util=new DjadeUtil();
        // NOW LETS START PROCESSING
        if(messaging!=null && subject!=null){
            // Now lets read
            try {
                data=util.readByScanner(TEMPLATESOURCE);
                // Now lets check
                if(data.length()>0){
                    // Here we start matching to replace
                    StringBuffer sb = new StringBuffer(data.length());
                    Pattern patA = Pattern.compile("dd%title%dd");
                    Pattern patB = Pattern.compile("dd%message%dd");

                    Matcher mA = patA.matcher(data);
                    Matcher mB = patB.matcher(data);

                    while (mA.find()) {
                        mA.appendReplacement(sb, subject);
                    }// End of while loop

                    while (mB.find()) {
                        mB.appendReplacement(sb, messaging);
                    }// End of while loop

                    //HERE WE STORE NEW CHANGE
                    mA.appendTail(sb);
                    mB.appendTail(sb);
                    content=sb.toString();
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        // Here we return string
        return content;
    }

Here is my html String now

<div style="line-height: 20px;">
                    <!-- THE BODY OF NEWSLATER GOES HERE //-->
                    <div class="newTitle">dd%title%dd</div>
                    <div class="newBody">dd%message%dd</div>
                    <div class="newButtons">
                        <button class="butNews" onclick="window.location='https://napoleoninvestment.net/?page_id=12'">Join our Telegram</button>
                        <button class="butNews" onclick="window.location='https://napoleoninvestment.net/?page_id=391'">Invest Now</button>
                    </div>
                </div>

I want to replace "dd%title%dd" with "This is the Title" and "dd%message%dd" with "This is the message body". I don't know where an going wrong. Any help will be appreciated


Solution

  • I think you forget to reassign data. You don't need replaceAll since there is no need for regex match.

    data = data.replace("dd%title%dd", "This is the Title");
    data = data.replace("dd%message%dd", "This is the message body");