dsmgcse8876 2016-09-01 09:18
浏览 33

使用正则表达式从Forth源代码中剥离注释

I am trying to match all content between parentheses, including parentheses in a non-greedy way. There should be a space before and after the opening parentheses (or the start of a line before the opening parentheses) and a space before and after the closing parentheses. Take the following text:

 ( )
  ( This is a comment )
    1 2 +
\ a
: square dup * ;
( foo bar 
baz )
(quux)
( ( )
(
( )

The first line should be matched, the second line including its content should be matched, the second last line should not be matched (or raise an error) and the last line should be matched. The two lines foo bar baz should be matched, but (quux) should not as it doesn't contain a space before and after the parentheses. The line with the extra opening parentheses inside should be matched.

I tried a few conventional regexes for matching content between parentheses but without much success. The regex engine is that of Go's.

  • 写回答

3条回答 默认 最新

  • douzha5990 2016-09-01 09:48
    关注
    re := regexp.MustCompile(`(?s)\(( | .*? )\)`)
    s = re.ReplaceAllString(s, "")
    

    Playground: https://play.golang.org/p/t93tc_hWAG

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效