ColdFusion OR条件下的正则表达式匹配

Rya*_*ill 1 regex coldfusion wiki parsing

我正在尝试编写一个解析wikiCreole文本的CF组件.我在使用我的正则表达式获取正确匹配时遇到问题.我觉得如果我能绕过第一个,其余的只需点击.这是一个例子:

以下是示例输入:

You can make things **bold** or //italic// or **//both//** or //**both**//.

Character formatting extends across line breaks: **bold,
this is still bold. This line deliberately does not end in star-star.

Not bold. Character formatting does not cross paragraph boundaries.
Run Code Online (Sandbox Code Playgroud)

我的第一次尝试是:

<cfset out = REreplace(out, "\*\*(.*?)\*\*", "<strong>\1</strong>", "all") />
Run Code Online (Sandbox Code Playgroud)

然后我意识到它不匹配**没有给出,它应该在有两个回车的地方结束.

所以我尝试了这个:

<cfset out = REreplace(out, "\*\*(.*?)[(\*\*)|(\r\n\r\n)]", "<strong>\1</strong>", "all") />
Run Code Online (Sandbox Code Playgroud)

它很接近,但由于某种原因,它给你这个:

You can make things <strong>bold</strong>* or //italic// or <strong>//both//</strong>* or //<strong>both</strong>*//.

Character formatting extends across line breaks: <strong>bold,</strong>
this is still bold. This line deliberately does not end in star-star.

Not bold. Character formatting does not cross paragraph boundaries.
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

PS:如果有人对更好的标签有任何建议,或者对这篇文章有更好的标题我都是耳朵.

Mic*_*man 6

[...]代表一个字符类,所以这一点:

[(\*\*)|(\r\n\r\n)]
Run Code Online (Sandbox Code Playgroud)

实际上与此相同:

[*|\r\n]
Run Code Online (Sandbox Code Playgroud)

即它匹配单个"*"和"|" 不是一个替代.

另一个问题是您更换双线换行.即使你的比赛成功,你最终也会合并段落.您需要恢复它或不首先使用它.我会用积极的先行来做后者.

在Perl中我会这样写:

$string =~ s/\*\*(.*?)(?:\*\*|(?=\n\n))/<strong>$1<\/strong>/sg;
Run Code Online (Sandbox Code Playgroud)

冒昧地猜测,ColdFusion可能看起来像这样:

REreplace(out, "\*\*(.*?)(?:\*\*|(?=\r\n\r\n))", "<strong>\1</strong>", "all")
Run Code Online (Sandbox Code Playgroud)