【发布时间】:2021-02-22 00:14:51
【问题描述】:
我需要帮助创建一个 c# 正则表达式模式,该模式将从字符串中查找“a”链接标记。
这是一个示例输入文本:
string text = "some line of text with random [ in the line. [http://example.com] [link title|http://example.com]";
我无法在 [http://example.com] 或 [link title|http://example.com] 上匹配
我尝试了很多失败的模式,这里只是最后两个:
@"\[(?<innerHtml>[.^\]]+)\|(?<href>[.^\]]+)\]\s"
@"\[(?<innerHtml>[^[]+.+)\]"
它们中的大多数返回一个匹配,如“字符串文本”所示。
in the line. [http://example.com] [link title|http://example.com
规格:
- 这是单行文本,不可能有\n\r。
- 第一个 [ 可以在行首,一行可以以 ] 结尾。
- 如果文本中存在 2 个链接标记,则它们之间将始终有一个空格“...] [...”。
| Markup | Replace |
|---|---|
[href] |
@"<a href=""${href}"">${href}</a>" |
[innerHtml|href] |
@"<a href=""${href}"">${innerHtml}</a>" |
【问题讨论】:
-
我认为您正在尝试这样做:
\[(?:(?<innerHtml>[^[|\]]+)\|)?(?<href>[^[\]]+)\]。这是demo。 -
完美!谢谢