doubo4824 2017-04-02 12:29
浏览 127
已采纳

如何用正则表达式和Golang替换可选组

I am trying to translate this:

{% img <right> /images/testing %}

into this:

{{< figure <class="right"> src="/images/testing" >}}

with regex in Golang. The part in <> in the source string is optional.

I have this code, which seems to work in the main test case, when the first capturing group exists ("right"):

regexp.MustCompile(`{%\s*img\s*(\p{L}*)\s+([/\S]+)\s+%}`)
.ReplaceAllString("{% img right /images/testing %}", "{{< figure class=\"$1\" src=\"$2\" >}}")

If the optional group is missing, however, I get:

{{< figure class="" src="/images/testing" >}}

which is not what I need - I want the entire class="" section gone, like this:

{{< figure src="/images/testing" >}}

Is this possible? Can I indicate somehow in the replacing string:

{{< figure class=\"$1\" src=\"$2\" >}}

that I want the additional text ("class=") gone if the optional group is empty?

  • 写回答

1条回答 默认 最新

  • doucu7525 2017-04-03 00:54
    关注

    Go regexp do not support conditional statements and the Replace family of regexp functions doesn't either. The solution to this depends on the number of special cases you have.

    If you only have the one case I'd suggest to just do a two pass replacement: First replace all occurences with the attribute set, then replace all the cases without the attribute (on play):

    txt := `{% img right /images/testing %}
    {% img /images/testing %}`
    
    // without attribute
    txt = regexp.MustCompile(`{%\s*img\s*([/\S]+)\s+%}`).
      ReplaceAllString(txt, "{{< figure src=\"$1\" >}}")
    
    // with attribute
    txt = regexp.MustCompile(`{%\s*img\s*(\p{L}*)\s+([/\S]+)\s+%}`).
      ReplaceAllString(txt, "{{< figure class=\"$1\" src=\"$2\" >}}")
    

    If you say this is inefficient I say: probably, yes. If you want something more efficient (i.e. something that does not iterate the source string twice) then you have to build something more akin to a parser which decides at the time of detection which format to use. A rough sketch of this would be something like this (on play):

    src := []byte("ok" + "{% img right /images/testing %}" + "this" + 
                  "{% img /images/testing %}" + "no?")
    dst := bytes.NewBufferString("")
    cidx := 0
    
    for _, match := range p.FindAllSubmatchIndex(src, -1) {
        dst.Write(src[cidx:match[0]])
        dst.WriteString(newFormat(src, src[match[2]:match[3]], src[match[4]:match[5]]))
        cidx = match[1]
    }
    dst.Write(src[cidx:])
    

    In this example you copy everything from your source text src to a buffer dst, replacing every occurrence of your pattern with the output of the value of a function. This function can then decide to include specific formatting or not.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因