Sublime Forum

Searching for a block of text

#1

I don’t even know if this is possible, but here goes.

My organization uses VSCode as its’ editor-of-choice (probably because they’re too stupid to use something that works !!), and I have a faece-load of Python code to remediate and munge into a format that somehoe will adhere to the “corporate standard”, where “corporate standard” is defined as “whatever the last Outsourced programmer used”.

Here’s what I need to search for;

Function function_name()
Line1
Line2
Etc
End Function

I need to find that as a block based on the function name, up to and including the End Function line.

Can’t seem to get regex quite right. Is it possible ???

0 Likes

#2

You can do this with a regex something like:

(?s)Function function_name\(.*?\).*?End Function

The (?s) turns on the option for . matching a newline; otherwise the regex will be rooted in the first line, and not work. That makes the dot operator be more greedy than you might expect, so both of .* constructs here are appended with ? to make them non-greedy; without that the match would start at the function you specify the name for and run through to the end of the last function in the file.

You may or may not need the .*? in the paranthesis if your functions don’t take any arguments. Similarly you could replace the name of the function with \w+ (or similar) to match all functions, and then you could skip through them one at a time.

0 Likes