function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Trif Cristian 7Trif Cristian 7 

replaceAll method doesn't work how it want

Hi, I have the following text:

    String target = '<br><br>I love computers and I love to program. <br>Do you love to program? <br><br>Or not?';
I want to replace all the `<br>`with the new line `\n`.

So I used this function:

    target = target.replaceAll('<br>', '\n');  but I have this error: Error on line 38, column 1: System.StringException: No match found. And if I try to replace with another string for example:

    target = target.replaceAll('<br>', 'test');   It works like a charm...

Here it's the full code for this:
 
String target = '<br><br>I love computers and I love to program. <br>Do you love to program? <br><br>Or not?';
    
    target = target.replaceAll('<br>', '\n');
    
    String regExp = '(.*)';
    Pattern myPattern = Pattern.compile(regExp);
    Matcher myMatcher = myPattern.matcher(target);
    
    System.debug('Matches: ' + myMatcher.matches());
    System.debug('Group 0: ' + myMatcher.group(1));

 
Newbie__SalesforceNewbie__Salesforce
Hey Triff,

you can use the following code
String target = '<br><br>I love computers and I love to program. <br>Do you love to program? <br><br>Or not?';
target = target.replace('<br>', '\n');
System.debug(target);

 
Trif Cristian 7Trif Cristian 7

Hi Newbie,

It shows correctly in the debug but I still have this error:

"Error on line 38, column 1: System.StringException: No match found." 

You know why?

Newbie__SalesforceNewbie__Salesforce
Can u share your code I guess ur string does not contains target characters
Trif Cristian 7Trif Cristian 7

Ignore the comment code

Ignore the commented code.:D

vinaykumar s 8vinaykumar s 8
Hi Trif,
 
   can you try below code 
   target = target.replace('<br>', '\\n');
   replace '\n' to '\\n' 

if this works let know pls
Trif Cristian 7Trif Cristian 7

@vinaykumar 

I tried like this also.. does not work.

I tried like this also: target = target.replaceAll('<br>', 'test'); and it works... apparently only \n does not like it. :D

vinaykumar s 8vinaykumar s 8
hey i tried that in my ogr console it is working fine 
code :
String target = '<br><br>I love computers and I love to program. <br>Do you love to program? <br><br>Or not?';
target = target.replace('<br>', '\\n');
String regExp = '(.*)';
Pattern myPattern = Pattern.compile(regExp);
Matcher myMatcher = myPattern.matcher(target);
System.debug('Group 0: ' + myMatcher);
System.debug('Group 0: ' + myPattern);
System.debug('Matches: ' + myMatcher.matches());
System.debug('Group 0: ' + myMatcher.group(0));

output:

User-added image