使用正则表达式查找包含特定方法或变量的函数 [英] Using Regex to find function containing a specific method or variable

查看:49
本文介绍了使用正则表达式查找包含特定方法或变量的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在stackoverflow上的第一篇文章,所以请对我温柔...

This is my first post on stackoverflow, so please be gentle with me...

我仍在学习正则表达式 - 主要是因为我终于发现了它们的用处,这部分是通过使用 Sublime Text 2.所以这是 Perl 正则表达式(我相信)

I am still learning regex - mostly because I have finally discovered how useful they can be and this is in part through using Sublime Text 2. So this is Perl regex (I believe)

我已经在这个网站和其他网站上搜索过,但我现在真的被困住了.也许我正在尝试做一些无法完成的事情

I have done searching on this and other sites but I am now genuinely stuck. Maybe I am trying to do something that can't be done

我想找到一个正则表达式(模式),它可以让我找到包含给定变量或方法调用的函数、方法或过程等.

I would like to find a regex (pattern) that will let me find the function or method or procedure etc that contains a given variable or method call.

我尝试了多种表达方式,它们似乎能起到一定的作用,但并非全部.特别是在 Javascript 中搜索时,我选择了多个函数声明,而不是最接近我要查找的调用/变量的那个.

I have tried a number of expressions and they seem to get part of the way but not all the way. Particularly when searching in Javascript I pick up multiple function declarations instead of the one nearest to the call/variable that I am looking for.

例如:我正在寻找调用方法 save data() 的函数我从这个优秀的网站了解到,我可以使用 (?s) 来切换 .包含换行符

for example: I am looking for the function that calls the method save data() I have learnt, from this excellent site that I can use (?s) to switch . to include newlines

function.*(?=(?s).*?savedata\(\))

然而,这将找到单词 function 的第一个实例,然后找到所有文本,包括 savedata()

however, that will find the first instance of the word function and then all the text unto and including savedata()

如果有多个过程,那么它将从下一个函数开始并重复,直到再次到达 savedata()

if there are multiple procedures then it will start at the next function and repeat until it gets to savedata() again

function(?s).*?savedata\(\) does something similar

我尝试使用以下方法要求它忽略第二个函数(我相信):

I have tried asking it to ignore the second function (I believe) by using something like:

function(?s).*?(?:(?!function).*?)*savedata\(\)

但这不起作用.

我已经做了一些向前看和向后看的调查,但要么我做错了(很有可能),要么它们不是正确的.

I have done some investigation with look forwards and look backwards but either I am doing it wrong (highly possible) or they are not the right thing.

总而言之(我猜),我如何倒退,从给定的单词到最近出现的不同单词.

In summary (I guess), how do I go backwards, from a given word to the nearest occurrence of a different word.

目前我正在使用它来搜索一些 javascript 文件以尝试了解结构/调用等,但最终我希望在 c# 文件和一些 vb.net 文件上使用

At the moment I am using this to search through some javascript files to try and understand the structure/calls etc but ultimately I am hoping to use on c# files and some vb.net files

非常感谢

感谢您的迅速回复,很抱歉没有添加示例代码块 - 我现在会这样做(已修改但仍足以显示问题)

Thanks for the swift responses and sorry for not added an example block of code - which I will do now (modified but still sufficient to show the issue)

如果我有一个简单的 javascript 块,如下所示:

if I have a simple block of javascript like the following:

    function a_CellClickHandler(gridName, cellId, button){
        var stuffhappenshere;
        var and here;
        if(something or other){
            if (anothertest) {

                event.returnValue=false;
                event.cancelBubble=true;
                return true; 
            }
            else{
                event.returnValue=false;
                event.cancelBubble=true;
                return true;
            }
        } 
    }

    function a_DblClickHandler(gridName, cellId){
        var userRow = rowfromsomewhere;
        var userCell = cellfromsomewhereelse;
        //this will need to save the local data before allowing any inserts to ensure that they are inserted in the correct place
        if (checkforarangeofthings){
            if (differenttest) {
                InsSeqNum = insertnumbervalue;
                InsRowID = arow.getValue()
                blnWasInsert = true;
                blnWasDoubleClick = true;
                SaveData();      
            }
        }
    }

针对此运行正则表达式 - 包括被确定为应该工作的第二个 Sublime Text 2 将选择从第一个函数到 SaveData() 的所有内容

running the regex against this - including the second one that was identified as should be working Sublime Text 2 will select everything from the first function through to SaveData()

在这种情况下,我希望能够只访问 dblClickHandler - 不能同时访问两者.

I would like to be able to get to just the dblClickHandler in this case - not both.

希望这个代码片段能增加一些清晰度,并且很抱歉没有发布最初,因为我希望标准代码文件就足够了.

Hopefully this code snippet will add some clarity and sorry for not posting originally as I hoped a standard code file would suffice.

推荐答案

此正则表达式将查找包含 SaveData 方法的每个 Javascript 函数:

This regex will find every Javascript function containing the SaveData method:

(?<=[\r\n])([\t ]*+)function[^\r\n]*+[\r\n]++(?:(?!\1\})[^\r\n]*+[\r\n]++)*?[^\r\n]*?\bSaveData\(\)

它将匹配函数中的所有行,包括包含 SaveData 方法的第一行.

It will match all the lines in the function up to, and including, the first line containing the SaveData method.

警告:

  • 源代码必须具有格式良好的缩进才能使其工作,因为正则表达式使用匹配的缩进来检测函数的结尾.
  • 如果函数从文件的第一行开始,则不会匹配该函数.

说明:

(?<=[\r\n])                      Start at the beginning of a line
([\t ]*+)                        Capture the indentation of that line in Capture Group 1
function[^\r\n]*+[\r\n]++        Match the rest of the declaration line of the function
(?:(?!\1\})[^\r\n]*+[\r\n]++)*?  Match more lines (lazily) which are not the last line of the function, until: 
[^\r\n]*?\bSaveData\(\)          Match the first line of the function containing the SaveData method call

注意: *+++ 是所有格量词,仅用于加速执行.

Note: The *+ and ++ are possessive quantifiers, only used to speed up execution.

修复了正则表达式的两个小问题.
修复了正则表达式的另一个小问题.

Fixed two minor problems with the regex.
Fixed another minor problem with the regex.

这篇关于使用正则表达式查找包含特定方法或变量的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆