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

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.

我是有关正则表达式的菜鸟(对不起)。 我正在尝试制作一种非常简单的匹配标记语言 粗体和斜体,然后将它们转换为HTML。 这是我正在使用的粗体示例: p>

  var粗体= regexp.MustCompile(“ \\ *([  ^ \\ *] +)\\ *“)
  code>  pre> 
 
 

它匹配两个星号之间的所有内容。 现在,我希望它匹配 * test * code>,但不匹配 \ * test * code>。 由于我对正则表达式了解不多,但我正在尝试进行此实验,所以我想知道这样做的方式。 我到处搜索,但找不到使这项工作有效的方法。 p> div>

Updated

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

(?:\A|(?:[^\\]+|\A)(\\{2})+|[^\\])\*([^\\*]+)\*