加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 数学中的错误:正则表达式应用于非常长的字符串

发布时间:2020-12-13 22:56:13 所属栏目:百科 来源:网络整理
导读:在下面的代码中,如果字符串被附加到10或2万个字符,则Mathematica内核可能会出现故障. s = "This is the first line.MAGIC_STRINGEverything after this line should get removed.1234567890123456789012345678901234567890123456789012345678901234567890123
在下面的代码中,如果字符串被附加到10或2万个字符,则Mathematica内核可能会出现故障.
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)*"->""]

我认为这主要是Mathematica的错,我已经提交了一个错误报告,如果我得到一个回应,将在后面跟进.但我也想知道我是否以愚蠢/低效的方式这样做.即使没有,数字化问题的想法也将不胜感激.

Mathematica使用PCRE语法,所以它确实有/ s也称为DOTALL aka Singleline修饰符,你只需要在你想要应用的表达式的部分之前加上(?s)修饰符.

请参阅这里的RegularExpression文档:(展开标题为“更多信息”的部分)
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

这个修改的输入并不会为我(原来的)使用一个长度为15,000个字符的字符串,为我创建了数学Mathematica 7.0.1,产生与你的表达式相同的输出:

s = StringReplace [s,RegularExpression @“.* MAGIC_STRING(?s).*” – >“”]

由于@AlanMoore解释的原因,它也应该更快一些

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读