我有这句话
#include "[yourDLL name].h"
我想获得Donec ante sapien, gravida id risus eget,
<exclude>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec quis neque ex. Aenean ac leo quis ex lobortis aliquam. Donec libero dolor, sodales in molestie vel, sagittis at nulla. Proin egestas dolor turpis. Aliquam erat volutpat. Nunc eget enim varius, condimentum tortor sit amet, aliquet magna. Phasellus ut libero quis diam dignissim interdum. Sed commodo iaculis vestibulum. Quisque viverra diam sed orci rhoncus luctus. Mauris leo mauris, consequat at lacinia sit amet, viverra vitae enim. Donec maximus, ipsum in bibendum volutpat, est erat dapibus leo, et iaculis arcu augue in dolor.
<exclude>
Donec ante sapien, gravida id risus eget,
标签之间的文本,我有这样的表达,但对我不起作用:<exclude>
标记甚至可以是/\<exclude\>/g
之类的东西,它只是文本那部分的分隔符。
如何解决此问题?
答案 0 :(得分:2)
类似的事情应该可以完成:
<exclude>((?:[^<]|<(?!exclude>))*)(?=<exclude>)
^---1---^|^----------2----------^|^-----3-----^
+-----------4-----------+
这指定了以下内容:
<exclude>
。<
或 <
后跟exclude>
的字符。<exclude>
的情况下,比赛才能结束。对于像标签#x
这样的更简单的东西,可以使用相同的主体:
#x((?:[^#]|#(?!x))*)(?=#x)
var text = `
Donec ante sapien, gravida id risus eget,
<exclude>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec quis neque ex. Aenean ac leo quis ex lobortis aliquam. Donec libero dolor, sodales in molestie vel, sagittis at nulla. Proin egestas dolor turpis. Aliquam erat volutpat. Nunc eget enim varius, condimentum tortor sit amet, aliquet magna. Phasellus ut libero quis diam dignissim interdum. Sed commodo iaculis vestibulum. Quisque viverra diam sed orci rhoncus luctus. Mauris leo mauris, consequat at lacinia sit amet, viverra vitae enim. Donec maximus, ipsum in bibendum volutpat, est erat dapibus leo, et iaculis arcu augue in dolor.
<exclude>
Donec ante sapien, gravida id risus eget,
`.trim();
var regex = /<exclude>((?:[^<]|<(?!exclude>))*)(?=<exclude>)/g;
while (match = regex.exec(text)) {
console.log(match[1]);
}
答案 1 :(得分:1)
var str =
"Donec ante sapien, gravida id risus eget,\n" +
"<exclude>\n" +
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec quis neque ex. Aenean ac leo quis ex lobortis aliquam. Donec libero dolor, sodales in molestie vel, sagittis at nulla. Proin egestas dolor turpis. Aliquam erat volutpat. Nunc eget enim varius, condimentum tortor sit amet, aliquet magna. Phasellus ut libero quis diam dignissim interdum. Sed commodo iaculis vestibulum. Quisque viverra diam sed orci rhoncus luctus. Mauris leo mauris, consequat at lacinia sit amet, viverra vitae enim. Donec maximus, ipsum in bibendum volutpat, est erat dapibus leo, et iaculis arcu augue in dolor.\n" +
"<exclude>\n" +
"Donec ante sapien, gravida id risus eget,\n"
var regex = /<exclude>([\S\s]*?)<exclude>/g;
var found = regex.exec( str )[1];
console.log( found );
答案 2 :(得分:0)
假设您的数据正确,并且实际上不是XML(应使用XML解析器代替)。给您特定的示例,这将起作用:
var str = `Donec ante sapien, gravida id risus eget,
<exclude>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec quis neque ex. Aenean ac leo quis ex lobortis aliquam. Donec libero dolor, sodales in molestie vel, sagittis at nulla. Proin egestas dolor turpis. Aliquam erat volutpat. Nunc eget enim varius, condimentum tortor sit amet, aliquet magna. Phasellus ut libero quis diam dignissim interdum. Sed commodo iaculis vestibulum. Quisque viverra diam sed orci rhoncus luctus. Mauris leo mauris, consequat at lacinia sit amet, viverra vitae enim. Donec maximus, ipsum in bibendum volutpat, est erat dapibus leo, et iaculis arcu augue in dolor.
<exclude>
Donec ante sapien, gravida id risus eget,`;
console.log(/<exclude>(.*?)<exclude>/gs.exec(str)[1]);
您将寻找与<exclude>
相匹配的内容,然后我们将所有内容归为下一个<exclude>
。尽管与此示例没有什么区别,但我们使用了一个惰性量词,因此,如果有多对<exclude>
标签,则您最多只能匹配 next { {1}},而不是最后一个<exclude>
。
答案 3 :(得分:0)
为此类输出设计表达式的一种方法是使用大多数答案已在使用的捕获组。
此外,您可以根据需要向表达式添加其他标志,例如多行或单行(取决于输入字符串)。 This tool可能会帮助您做到这一点,设计和测试您的表情,并查看它们如何工作:
(.*)(<exclude>)(.+)(<exclude>)(.*)
此图显示了表达式的工作方式:
此JavaScript代码段使用简单的100万次for
循环来显示此表达式的性能。
repeat = 1000000;
start = Date.now();
for (var i = repeat; i >= 0; i--) {
var string = "Donec ante sapien, gravida id risus eget,\n" +
"<exclude>\n" +
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec quis neque ex. Aenean ac leo quis ex lobortis aliquam. Donec libero dolor, sodales in molestie vel, sagittis at nulla. Proin egestas dolor turpis. Aliquam erat volutpat. Nunc eget enim varius, condimentum tortor sit amet, aliquet magna. Phasellus ut libero quis diam dignissim interdum. Sed commodo iaculis vestibulum. Quisque viverra diam sed orci rhoncus luctus. Mauris leo mauris, consequat at lacinia sit amet, viverra vitae enim. Donec maximus, ipsum in bibendum volutpat, est erat dapibus leo, et iaculis arcu augue in dolor.\n" +
"<exclude>\n" +
"Donec ante sapien, gravida id risus eget,\n";
var regex = /(.+)(<exclude>)(.+)(<exclude>)(.+)/gms;
var match = string.replace(regex, "$3");
}
end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. ");