正则表达式检测边界,例如源代码行上的注释

时间:2012-01-06 11:31:32

标签: c++ regex

是否可以创建一个正则表达式,它将返回以下行的匹配/不匹配?

/*8th*/break;  match

break;/*8th*/ match 

/*8th*/ no match
/*8th no match
8th*/ no match  
// /* some_comment    no match  
// some_comment no match
some_comment*/ no match
some_comment // match

如果是的话,请你提供这个正则表达式吗?

基本上我想要抓住的是除了评论之外还有什么内容。

1 个答案:

答案 0 :(得分:0)

你不能用正则表达式“真正”做到这一点。您可以获得80%的案例,但这是一个复杂的问题。例如,如果你有一个带引号的字符串“* /” - 正则表达式只是看* /并认为评论正在结束。

您可能会看到语法高亮显示器如何做到这一点。或者,check this burly AWK script out

相关问题