11

I've got a project where I'd like to replicate some of the internal merge field functionality so users can build a link and it'd be really helpful if I could do a case-insensitive string replaceAll.

For example, if I want to replace {!id} or any of it's case variations I could do

return input.replaceAll('\\{![iI][dD]}\\', value);

While I can make do with creating a function to built a regex for any particular input pattern (nevermind the potential issues with script statements) it'd be great if I could emulate how sed works (the 'i' indicates case insensitive). For example:

sed 's/\{!id\}/replacement/i' inputfile.txt

Anyone have any tips on how to accomplish this?

4 Answers 4

13

The option to replace case insensitive regex in java is Pattern.CASE_INSENSITIVE, which can also be specified as (?i)

(http://boards.developerforce.com/t5/Apex-Code-Development/Why-Pattern-CASE-INSENSITIVE-not-detectable/td-p/204337)

String srcStr = '{!id} value {!Id} is not {!ID}';
String replaceToken = '(?i)\\{!id\\}';
System.debug('REPLACEMENT ' + srcStr.replaceAll(replaceToken, ''));
System.assertEquals('  value  is not ', srcStr.replaceAll(replaceToken, ''));
1
  • For a 'Mortarboard' encore ;) Commented Nov 9, 2012 at 23:08
2

Just curious can you not convert the string to a particular case?

say

String ex = 'Upper';
ex.toLowerCase();

(or)

ex.toUpperCase()

Examples from the official documentation -

String s1 = 'ThIs iS hArD tO rEaD';
System.assertEquals('this is hard to read',
s1.toLowerCase());

String myString1 = 'abcd';
String myString2 = 'ABCD';
myString1 = 
   myString1.toUpperCase();
Boolean result = 
   myString1.equals(myString2);
System.assertEquals(result, true);
3
  • The string gets displayed to the end user, so I'd need to convert it back to it's original case ... Commented Nov 9, 2012 at 22:20
  • 1
    Store the original in a string - have a separate variable where you upper/lowercase the values... do your work... replace with original String value? Commented Nov 9, 2012 at 22:36
  • 1
    I would agree to the above comment and since you mentioned the {!Id} field FYI : doing the conversion of Id would result in troubles sometimes if you use the 18 digit ID because 15 digit is a case sensitive ID forceguru.blogspot.com/2010/12/…
    – Rao
    Commented Nov 9, 2012 at 22:58
0

You can use other salesforce string methods for the replacement. For example:

String inputString = 'Welcome {!Contact.Name}';
String toReplace = 'contact.name';  //Can use describe methods and get all fields for the relevant object/s

while(inputString.indexOfCaseIgnoreCase(toReplace) != -1){
    Integer indexStartReplace = inputString.indexOfCaseIgnoreCase(toReplace);
    Integer indexEndReplace = indexStartReplace + toReplace.length()
    inputString = inputString.replace(inputString.substring(indexStartReplace, indexEndReplace), '');
}
-1

Salesforce Apex String replacement: Use the following regex to ignore case sensitivity in replace operation.

***//Use (?i) regex to make the replacement process to avoid case sensitive***   
String message = 'Welcome Home';
String searchText = 'home';
String newText = 'World';     
               
String convertedText = message.replaceAll('(?i)'+searchText, newText); 
System.debug('convertedText>> '+ convertedText);
***//Output: convertedText>> Welcome World***
2
  • Honestly, why do you think your answer (to a question that is 10 years old) does contribute anything to the accepted answer?! Commented Mar 24, 2022 at 9:32
  • I posted something, that has a different way than others in this question forum. so when people are searching they might get different ways to solve their problems. To be frank, do you think every problem has only one solution? it's not about acceptable or not.
    – Senthamizh
    Commented Mar 25, 2022 at 11:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .