【问题标题】:Regex for markdown url on all urls in golanggolang中所有url上的markdown url的正则表达式
【发布时间】:2015-07-09 23:17:19
【问题描述】:

我正在尝试处理一个降价文件,替换所有图片网址。 markdown 图片 url 格式为![alternative name](imageurl.png)

我的正则表达式搜索找到第一个,返回位置并替换它,然后循环遍历文档,直到我的正则表达式搜索未找到任何内容 - 即其匹配维度数组为空。

问题是由于某种原因它继续匹配“我不完全知道什么”。即从正则表达式搜索返回的数组的长度永远不会是 0

location := split[:locationSplit]

bodyRe := regexp.MustCompile(`!\[(.*)\]\((.*)\)`)
indexes := bodyRe.FindStringIndex(body)
fmt.Println("location: ", absoluteFileLocation)
fmt.Println("length: ", indexes)

for len(indexes) != 0 {
    fmt.Println("length: ", len(indexes))
    imageLocation := body[indexes[0]:indexes[1]]
    body = body[:indexes[0]] + imageLocation + body[indexes[1]:]
    indexes = indexes[:0]
    fmt.Println("length: ", len(indexes))
    indexes = bodyRe.FindStringIndex(body)
}

这会返回一个连续的:

length:  2
length:  0
length:  2
length:  0
length:  2
length:  0
length:  2
length:  0
length:  2

2 来自循环内的 indexes = bodyRe.FindStringIndex(body) 行,因为我之前将索引设置为 0。

帮助赞赏

编辑:根据请求进行编辑,例如包括在内。上面的方法明显有缺陷,下面这个方法适用于第一张图片,但不适用于下一张

所以我尝试了这种技术:

(示例降价文件)

some markdown

![image](anImage.png)

more markdown

![image2](anImage2.png)

more markdown & end of document

以及修改后的代码:

...
...
    bodyRe := regexp.MustCompile(`!\[(.*)\]\((.*)\)`)
    indexes := bodyRe.FindAllStringSubmatchIndex(body, -1)

    for _, j := range(indexes) { //i is the index, j is the element (in this case j = []int )
        imageLocation := body[j[4]:j[5]]
        body = body[:j[4]] + "/App/Image/?image=" + location + "/" + imageLocation + body[j[5]:]
    }
    return body

(必需的输出降价)

some markdown

![image](/App/Image/?image=[location]/anImage.png)

more markdown

![image2](/App/Image/?image=[location]/anImage2.png)

more markdown
end of document

这适用于第一张图片。但不是第二个。问题是(我认为当该方法循环并替换第一个时,正文中的索引(即body[j[4]:j[5]])会发生变化,因此它会在错误的位置替换第二个。

我需要这样做,以便在最终呈现降价时,图像 url 指向可以提供它们的位置。

编辑:已修复

谢谢各位。 由于人们很难理解我想要做什么,我怀疑我正在以一种奇怪的方式解决这个问题。我已经让它工作了,下面是代码 sn-p,它适用于任何其他调查此问题的人。

首先,我将解释我遇到问题的原因。 我想将网站的博客写作与网站本身的实际维护分开。因此,“博客作者”被告知用 markdown 编写博客,所有图像标签的格式为 `,其中所有图像必须与 markdown 文件本身位于同一目录中。 由于此目录不是网站本身代码库的一部分,因此需要将图像 url 替换为绝对 url 以便可以提供它们。我不希望这成为博客作者需要担心的事情。

第一张图片一切正常,但是因为替换的绝对 URL 改变了长度,因此博客内容中所有字符的位置,正则表达式找到的索引不再对齐,所以我不得不添加匹配索引的新长度。

adjustment := 0
for _, j := range(indexes) {
    imageLocation := body[j[4]+adjustment:j[5]+adjustment]

    replacement := "?imageurl=" + url.QueryEscape(location) + "/" + imageLocation
    body = body[:j[4] + adjustment] + replacement + body[j[5] + adjustment:]
    adjustment += len(replacement) - len(imageLocation)
}

【问题讨论】:

  • 我没有时间深入研究这个,但是如果停止重新使用 indexes 切片会发生什么?我对该代码持怀疑态度。乍一看,你正在做一堆重新切片,因为我必须停下来想想每一行代码之后集合是什么样的,这可能是你的错误发生的地方。
  • 是否可以根据样本输入和所需输出重述问题?
  • Markdown 图片链接也可以指定一个标题:![alt text](/path/to/img.jpg "Title") 那么![alt text][id] 之后的[id]: /path/to/img.jpg "Title" 的完全有效的markdown 呢?尝试使用正则表达式解析事物通常是困难且容易出错的。当然,Go 中必须有一个可用的降价解析器。
  • 是的,有一个降价解析器。问题是我正在托管从 golang Web 服务器显示的降价和图像。服务器需要知道图像的位置。当markdown的作者放置相对路径时,我需要将其更改为绝对路径。不过你是对的,所以我将降价作者限制为仅图像相对位置,并且图像必须与降价文件本身位于同一目录中。

标签: regex go infinite-loop


【解决方案1】:

在这一行之后:

imageLocation := body[indexes[0]:indexes[1]]

imageLocation 将包含类似![image](anImage.png) 的字符串。

body = body[:indexes[0]] + imageLocation + body[indexes[1]:]

在该行之后,正文将与之前相同。您基本上是从 3 段中重建它。

这相当于做the following:

package main

import "fmt"

func main() {
    s := "Hello, playground"
    t := s[2:4]
    s = s[:2] + t + s[4:]
    fmt.Println(s) // prints "Hello, playground"
}

在下一次迭代中,将再次找到相同的最左侧匹配项,永无止境。

您是否阅读过FindStringIndex 的文档?

如果您编辑您的问题以说出您想要做什么,我可以为您提供一个工作代码 sn-p。

【讨论】:

  • 我现在用工作版本更新了原始问题。谢谢
猜你喜欢
  • 2021-12-18
  • 1970-01-01
  • 2011-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多