In the following code, if the string s is appended to be something like 10 or 20 thousand characters, the Mathematica kernel seg faults.
s = \"This is the fi
The best way to optimize the regex depends on the internals of Mathematica's regex engine, but I would definitely get rid of the (.|\\n)*
, as @Simon mentioned. It's not just the alternation--although it's almost always a mistake to have an alternation in which every alternative matches exactly one character; that's what character classes are for. But you're also capturing each character when you match it (because of the parentheses), only to throw it away when you match the next character.
A quick scan of the Mathematica regex docs doesn't turn up anything like the /s
(Singleline or DOTALL) modifier, so I recommend the old JavaScript standby, [\\s\\S]*
-- match anything that is whitespace or anything that isn't whitespace. Also, it might help to add the $
anchor to the end of the regex:
"(^|\\n)[^\\n]*MAGIC_STRING[\\s\\S]*$"
But your best option would probably be not to use regexes at all. I don't see anything here that requires them, and it would probably be much easier as well as more efficient to use Mathematica's normal string-manipulation functions.