Regex检测边界,例如源代码行上的注释

Regex detecting boundaries such as comments on the line of source code

本文关键字:注释 源代码 检测 边界 Regex      更新时间:2023-10-16

是否可以创建一个正则表达式,它将为以下行返回match/no-match?

/*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

如果是,你能给我提供这个正则表达式吗?

基本上,我想了解的是,除了评论之外,是否还有其他内容。

使用正则表达式不能"真正"做到这一点。你可以得到80%的案例,但这是一个复杂的问题。例如,如果您有一个带引号的字符串"*/",regex只会看到*/并认为注释正在关闭。

你可能会很幸运地看到语法高亮器是如何做到这一点的。或者,看看这个庞大的AWK脚本。