Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 检查正则表达式匹配是否在多行注释中_Regex - Fatal编程技术网

Regex 检查正则表达式匹配是否在多行注释中

Regex 检查正则表达式匹配是否在多行注释中,regex,Regex,我在一个文件中有一组匹配项,希望找出哪些匹配项在注释中(介于/*和*/之间)。这是我可以用正则表达式做的吗 我考虑的是/\*.*匹配。*\*/,但是,这也符合情况/*注释*/MATCH/*其他注释*/。有没有正确的方法可以做到这一点 请注意,MATCH可以在文件中多次出现(在注释中,而不是在注释中) 您可以在此处使用基于lookahead的正则表达式。请参阅演示 这是因为*是贪婪的,将匹配尽可能多的字符。相反,请尝试使用惰性匹配*?: \/\*[^\/\*]*?MATCH[^\/\*]*?(\*

我在一个文件中有一组匹配项,希望找出哪些匹配项在注释中(介于
/*
*/
之间)。这是我可以用正则表达式做的吗

我考虑的是
/\*.*匹配。*\*/
,但是,这也符合情况
/*注释*/MATCH/*其他注释*/
。有没有正确的方法可以做到这一点

请注意,
MATCH
可以在文件中多次出现(在注释中,而不是在注释中)

您可以在此处使用基于
lookahead
的正则表达式。请参阅演示


这是因为
*
是贪婪的,将匹配尽可能多的字符。相反,请尝试使用惰性匹配
*?

\/\*[^\/\*]*?MATCH[^\/\*]*?(\*\/)

以下是一个。

虽然previos先行查找答案有效,但它不适用于文件中的多个匹配事件。因此,我找到了一个更好的、基于非正则表达式的解决方案:

var lastCommentStartBefore = content.LastIndexOf("/*", match.Index);
var lastCommentEndBefore = content.LastIndexOf("*/", match.Index);
if (lastCommentEndBefore < lastCommentStartBefore)
    // within comment
var lastCommentStartBefore=content.LastIndexOf(“/*”,match.Index);
var lastCommentEndBefore=content.LastIndexOf(“*/”,match.Index);
if(lastCommentEndBefore
请在问题中附上相关代码。这确实是一个糟糕的做法,没有提供所有相关的细节,然后发布一个答案,其中包含一段不完整的代码。我添加了“匹配”可能出现多次的细节。我不知道该在问题中添加哪一个代码。答案中的代码不完整…不完整?你用什么语言写的?
匹配变量的类型是什么?如何申报?它装的是什么?
var lastCommentStartBefore = content.LastIndexOf("/*", match.Index);
var lastCommentEndBefore = content.LastIndexOf("*/", match.Index);
if (lastCommentEndBefore < lastCommentStartBefore)
    // within comment