正则表达式单行注释 [英] Regex for single line comments

查看:66
本文介绍了正则表达式单行注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用正则表达式来标识评论.它必须以//开头,并以新行或 *)模式结束.

I'm trying to make a regex to identify a comment. It has to start with // and end with a new line or a *) pattern.

现在,我设法获得了这个(\/\/)([^ \ n \ r] +),但是我没有成功添加 *)模式.

For now, I manage to get this (\/\/)([^\n\r]+), but I am unsuccessful to add the *) pattern.

有什么提示吗?

推荐答案

尝试如下: ^ \/\/[^ \ n \ r] +(?:[\ n \ r] | \ * \))$

Try it like this: ^\/\/[^\n\r]+(?:[\n\r]|\*\))$

比赛

  • ^ 字符串的开头
  • \/\/匹配两个正斜杠
  • [^ \ n \ r] + 不匹配换行符或回车符1次或更多次
  • (?:非捕获组
    • [\ n \ r] | \ * \)匹配换行符或回车符或*)
    • ^ Beginning of the string
    • \/\/ Match two forward slashes
    • [^\n\r]+ Match not a newline or a carriage return 1 or more times
    • (?: Non capturing group
      • [\n\r]|\*\) Match a newline or a carriage return or *)

      根据评论进行了更新,这是最终的正则表达式:

      Updated according to the comments, this is the final regex:

      \/\/[^ \ n \ r] +?(?:\ *\)| [\ n \ r])

      这篇关于正则表达式单行注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆