Bug in Mathematica: regular expression applied to very long string

前端 未结 3 1891
深忆病人
深忆病人 2021-01-02 07:14

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         


        
3条回答
  •  一个人的身影
    2021-01-02 07:48

    Mathematica uses PCRE syntax, so it does have the /s aka DOTALL aka 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

    The following set options for all regular expression elements that follow them:
    (?i) treat uppercase and lowercase as equivalent (ignore case)
    (?m) make ^ and $ match start and end of lines (multiline mode)
    (?s) allow . to match newline
    (?-c) unset options

    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

提交回复
热议问题