正则表达式查找C注释 [英] Regexp to find C comments
问题描述
我需要在Ruby中使用正则表达式才能在C指令之前找到注释.
I need a regexp in Ruby to find the comment before a C instruction.
例如,我有此文件example.c
/*
* COMMENT NUMBER 1
*/
x = rb_define_class_under (foo, "MyClassName1", bar);
/*
* COMMENT NUMBER 2
*/
y = rb_define_class_under (foo, "MyClassName2", bar);
/*
* COMMENT NUMBER 3
*/
z = rb_define_class_under (foo, "MyClassName3", bar);
然后我将解析器放在ruby parser.rb
中,如下所示:
Then I have my parser in ruby parser.rb
like this:
content = File.open('example.c').read
if content =~ /((?>\/\*.*?\*\/))([\w\.\s]+\s=\s)?rb_define_class_under.*?"(MyClassName1)"/m
puts "Comment number 1 is:"
puts $1
end
if content =~ /((?>\/\*.*?\*\/))([\w\.\s]+\s=\s)?rb_define_class_under.*?"(MyClassName2)"/m
puts "Comment number 2 is:"
puts $1
end
if content =~ /((?>\/\*.*?\*\/))([\w\.\s]+\s=\s)?rb_define_class_under.*?"(MyClassName3)"/m
puts "Comment number 3 is:"
puts $1
end
现在我期望的输出是这样:
Now the output I expect is this:
Comment number 1 is:
/*
* COMMENT NUMBER 1
*/
Comment number 2 is:
/*
* COMMENT NUMBER 2
*/
Comment number 3 is:
/*
* COMMENT NUMBER 3
*/
但是我得到了
Comment number 1 is:
/*
* COMMENT NUMBER 1
*/
Comment number 2 is:
/*
* COMMENT NUMBER 1
*/
Comment number 3 is:
/*
* COMMENT NUMBER 1
*/
有什么主意吗?什么是正确的正则表达式以获取预期的输出?
Any idea? What is the right regexp to get the expected output?
推荐答案
尝试将.*
添加到正则表达式的开头.
Try adding .*
to the beginning of the regex.
当前,正则表达式中rb_define_class_under
之后的.*?
会导致您始终匹配并捕获字符串的第一部分,并且.*?
会一直匹配,直到您真正要查找的类名为止.
Currently the .*?
after rb_define_class_under
in your regex is causing you to always match and capture the first part of the string, and the .*?
matches up until the class name you are actually looking for.
通过在正则表达式的开头添加贪婪的匹配项,可以确保仅在想要的类名之前的最后一个/*
处开始捕获组.
By adding a greedy match at the beginning of the regex you make sure that you only start your capture group at the last /*
before the class name you want.
示例: http://www.rubular.com/r/Orja089zAI
请注意,您仍然从字符串的开头开始进行匹配,但是第一个捕获组是正确的注释.
Note that you still match from the beginning of the string, but the first capture group is the correct comment.
这篇关于正则表达式查找C注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!