I am working on an apex class that makes a soap API callout and receives a response. To see the complete response in debug logs, I am using a splitString() method to break down the XML response at nth character. However, depending on the API response, the splitString() method is failing with the error message "REGEX too complicated".

Below is the implementation of the splitString() method.

    public static string splitString(String strInput, String delimiter, Integer charactersBetween) {
    Matcher m = Pattern.compile('.{1,' + charactersBetween + '}').matcher(strInput);
    List<String> chunks = new List<String>();
    while (m.find()) chunks.add(m.group());
    return String.join(chunks, delimiter);

while calling this method, I am calling as shown below.


The statement will split the Soap API response at 132nd character and adds a newline character, making it a multi-line response so that it can be displayed in debug logs.

However, at times, this method is failing with "REGEX too complicated" error message. Can someone help?

  • You already have a Dom.Document, it looks like. Why not just use the various XmlNode methods to debug the data using a simple recursive function?
    – sfdcfox
    Nov 7, 2022 at 14:57

1 Answer 1


If you want to find the value between two particular flags, I realised recently with the new String methods a few years ago there's one called substringBetween which can be a handy way of grabbing some values out of long-winded XML without bothering to parse it all etc.:

String body = response.getBody();
String token = body.substringBetween('<token>', '</token>');

Of course you could write a method to do this yourself, but this is short and sweet and seems to work on some pretty long XML strings.

You must log in to answer this question.

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