简体   繁体   English

如何在ruby中将代码注释与正则表达式匹配?

[英]How to match code comment with regex in ruby?

given below sample code 在下面的示例代码中给出

      get => "hello#index" # comments

How to macth '# comments' ? 如何处理“#注释”?

There is Ruby regular expression editor http://rubular.com/ for test. 有Ruby正则表达式编辑器http://rubular.com/进行测试。

Assuming the unrealistic case in which you want to match any text at all after the last hash you can use 假设您不希望在最后一个散列之后完全匹配任何文本的情况,可以使用

/#[^#]*$/

But there is no guarantee the last hash actually begins a comment. 但是不能保证最后的哈希实际上会开始注释。 If a line had a hash inside a string literal or was itself used as a delimiter in a %w , for example 例如,如果某行在字符串文字中包含哈希值,或者本身已用作%w的定界符,

%w# abc def #

or was used in something like this 或用在这样的东西

dog = "spike"
%w{#dog rat}

then you will have a difficult time trying to come up with a regex. 那么您将很难找到正则表达式。 I would go with a ruby parser. 我会去一个红宝石解析器。

/# comments/可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM