dsfsfdsf4544 2016-07-30 19:59
浏览 614
已采纳

Golang中的正则表达式:如何设置使字符串不匹配的字符?

I am a noob about regular expressions (sorry). I was trying to make a very simple markup language that matches bold and italic and then converts them to HTML. Here is an example for bold that I'm using:

var bold = regexp.MustCompile("\\*([^\\*]+)\\*")

It matches everything between two asterisks. Now, I'd like it to match *test* but not \*test*. Since I don't know much about regular expressions but I'm trying to make this experiment, I'd like to know what's the way for that. I searched everywhere but couldn't find the way to make this work.

  • 写回答

1条回答 默认 最新

  • douyu9012 2016-07-30 20:28
    关注

    Updated

    Go does not support lookbehinds. So a workaround can be:

    (?:\A|(?:[^\\]+|\A)(\\{2})+|[^\\])\*([^\\*]+)\*
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?