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 first line.
MAGIC_STRING
Everything after this line should get removed.
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890123456789012345678901234567890
...";
s = StringReplace[s, RegularExpression@"(^|\\n)[^\\n]*MAGIC_STRING(.|\\n)*"->""]
I think this is primarily Mathematica’s fault and I’ve submitted a bug report and will follow up here if I get a response. But I’m also wondering if I’m doing this in a stupid/inefficient way. And even if not, ideas for working around Mathematica’s bug would be appreciated.
Mathematica uses PCRE syntax, so it does have the
/sakaDOTALLaka Singleline modifier, you just prepend the(?s)modifier before the part of the expression in which you want it to apply.See the RegularExpression documentation here: (expand the section labeled “More Information”)
http://reference.wolfram.com/mathematica/ref/RegularExpression.html
This modified input doesn’t crash Mathematica 7.0.1 for me (the original did), using a string that is 15,000 characters long, producing the same output as your expression:
s = StringReplace[s,RegularExpression@".*MAGIC_STRING(?s).*"->""]It should also be a bit faster for the reasons @AlanMoore explained