You need to sign in to do that
Don't have an account?

How to replace string with regex pattern, matcher and replaceFirst ? (Regex too complicated)
Hi.
I want to replace some specific words in string with anothers using pattern and matcher. Example: I want format nicely SOQL query so:
[ SELECT Id, Name FROM Account WHERE Name = 'John' ]
should be changed to:
[SELECT
Id, Name
FROM
Account
WHERE
Name = 'John']
The code:
String convertInput = '[ SELECT Id, Name FROM Account WHERE Name = \'John\' ]'; String convertOutput = ''; String regExp = '(SELECT|FROM|WHERE)'; Pattern p = Pattern.compile(regExp); Matcher m = p.matcher(convertInput); while (m.find() == true) { convertOutput = m.replaceFirst('\n' + m.group(1) + '\n'); } System.debug(convertOutput);
Gives error: System.LimitException: Regex too complicated
Where code:
String convertInput = '[ SELECT Id, Name FROM Account WHERE Name = \'John\' ]'; String convertOutput = ''; String regExp = '(SELECT|FROM|WHERE)'; Pattern p = Pattern.compile(regExp); Matcher m = p.matcher(convertInput); while (m.find() == true) { convertOutput = m.replaceAll('\n' + m.group(1) + '\n'); } System.debug(convertOutput);
Gives output:
SELECT
Id, Name
SELECT
Account
SELECT
Name = 'John' ]
So I am confused what is so complicated in replaceFirst?
Do you have any suggestions how to solve that?
All Answers
I wanted to add a prefix as abc__xyz__c as I insert a string xyz__c
using custom controller