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
Randi WarnerRandi Warner 

String indexOf() manipulation

I have a script that is hitting a System.LimitException: Regex too complicated Error and so to combat this I've come up with a solution to chunk up the string and then do the regex to the chunks so that it wont hit that limit. 
Here is the problem, finding a clean way to cut up the document so that it still works. 
Originally I was cutting the string up by every new line String.split('\r\n|\r|\n') so it could then digest it line by line (but if the document was too long it hit the above limit) so now im targeting some text and then want to get the next new line that comes after so it will be a clean cut. Then once I get a chunk, i'll apply the new line regex and add it to a list then keep doing that for each chunk so I can achive the same result and work around the limit.

so for example:

If myString = '22:45:07.0 (1307331)|HEAP_ALLOCATE|[139]|Bytes:6
22:45:07.0 (1328106)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:27
22:45:07.0 (1358441)|METHOD_ENTRY|[2]|01p4x000006c0jb|AttachmentTriggerLogic_Test.AttachmentTriggerLogic_Test()
22:45:07.0 (1374280)|STATEMENT_EXECUTE|[2]
22:45:07.0 (1383574)|STATEMENT_EXECUTE|[2]
22:45:07.0 (1386012)|STATEMENT_EXECUTE|[4]'

and I target the index of ')|METHOD_ENTRY|', how then would i be able to take that index and find the next index of a match? 

Integer firstMatch = myString.indexOf(')|METHOD_ENTRY|');
String newLineRegex = '\r\n|\r|\n';
Integer secondMatch = *new line after first match*?

Let me know if anyone has any ideas?
Randi WarnerRandi Warner
I was thinking of using String.indexOf(substring, index) but I need it to be String.indexOf(REGEX, index) and when I try it using a regex it returns -1 as it cant find the regex (since thats not a sub string lol) I just need to find the posion of the end of line after the firstIndex I match. 
Randi WarnerRandi Warner
I think the best solution would be to match the substring in the string.split regex and then in that same regex find the new line after that. Im just not that experienced with regex to do it lol but I know it can be done.