Vbscript正则表达式 - 匹配[nextpage]

时间:2016-02-23 23:22:22

标签: regex vbscript asp-classic

我正在尝试从CMS获取输入并将其转换为多个页面。我正在尝试编写一个正则表达式,通过查找 [nextpage] 将每个“页面”提取到一个数组中。我可以非常接近,但输出并不是我想要的。

假设此内容为例:

我们美国人民,为了形成一个更完美的联盟,建立正义,保证国内宁静,提供共同防御,促进一般福利,并确保自由的祝福[下一页]对我们自己和我们的后人,为美利坚合众国制定宪法并为此制定宪法。

我当前的正则表达式:

/(.*?)\[nextpage](.*?)/

我想要的结果:

[0] =我们是美国人民,为了形成

[1] =一个更完美的联盟,建立正义,确保国内宁静,提供共同防御,促进一般福利,并确保自由的祝福

[2] =对我们自己和我们的后人,为美利坚合众国制定本宪法。

谢谢。

2 个答案:

答案 0 :(得分:3)

这似乎更容易。

Dim B
A = "Whereas the people of New South Wales, Victoria, South Australia, Queensland, and Tasmania, humbly relying on the blessing of Almighty God, have agreed to unite in one indissoluble Federal Commonwealth under the Crown of the United Kingdom [nextpage] of Great Britain and Ireland, [nextpage] and under the Constitution hereby established:"
B = Split(A, "[nextpage]")
Count = 0
For each term in B
    msgbox Count & " = " & term
    Count = count+1
Next

答案 1 :(得分:1)

从我的评论中发布此信息。

however there is always an empty result at the end.

当然,.*?允许[nextpage][nextpage]之间的空内容。

如果您想删掉尾随空格,请使用(.*?)(?:\[nextpage\]|$)(?<=.)

这也将在最后修复额外的空匹配。

更新VBscript

显然VBscript与JScript是同一个垃圾。

在这种情况下,您必须使用此(.*?(?=\[nextpage\])|.+)(?:\[nextpage\]|$)

注意 - 如果您希望匹配跨越行,则可以使用[\S\s]代替正则表达式中的点.

([\S\s]*?(?=\[nextpage\])|[\S\s]+)(?:\[nextpage\]|$)