admin 管理员组

文章数量: 1087132

I am looking through a code base for a specific if statement used as a gate, that is followed by a certain function. IE if global_var contains test.check()

if global_var:
   fizz()
   test.buzz()
   test.check()
   bang()
   bop()

would match but not any of the following

fizz()
test.buzz()
test.check()
bang()
bop()
if global_var:
   fizz()
   test.buzz()
   bang()
   bop()

If possible, I would like to do so in the search bar of my IDE or github website, not with another script. So a Regex implementation would be ideal. I figure that a Regex that matches two different strings with N lines of each other could be possible, and applicable in this circumstance given we have a 30 line limit on how long functions can be; but I do not know how to write a statement that checks across multiple lines of code after the first match, or how to limit it not to go to the end of the file.

Is this something that is possible with regex? If so, could i get a generic example/explanation to help me understand how? If not I will consign myself to writing another bash script to do this search.

I am looking through a code base for a specific if statement used as a gate, that is followed by a certain function. IE if global_var contains test.check()

if global_var:
   fizz()
   test.buzz()
   test.check()
   bang()
   bop()

would match but not any of the following

fizz()
test.buzz()
test.check()
bang()
bop()
if global_var:
   fizz()
   test.buzz()
   bang()
   bop()

If possible, I would like to do so in the search bar of my IDE or github website, not with another script. So a Regex implementation would be ideal. I figure that a Regex that matches two different strings with N lines of each other could be possible, and applicable in this circumstance given we have a 30 line limit on how long functions can be; but I do not know how to write a statement that checks across multiple lines of code after the first match, or how to limit it not to go to the end of the file.

Is this something that is possible with regex? If so, could i get a generic example/explanation to help me understand how? If not I will consign myself to writing another bash script to do this search.

Share Improve this question edited Mar 27 at 14:44 Barmar 784k57 gold badges548 silver badges660 bronze badges asked Mar 27 at 14:24 FalderolFalderol 1963 silver badges14 bronze badges 10
  • 1 Something like ^if global_var:$\n(.*\n){0,30}?.*test\.check\(\) – DuesserBaest Commented Mar 27 at 14:31
  • That didn't quite work, but using this I was able to ask copilot for help, and get (pattern1(?:[^\n]*\n){0,30}?pattern2|pattern2(?:[^\n]*\n){0,30}?pattern1) which is working. But i dont understand all of it. – Falderol Commented Mar 27 at 14:52
  • 1 (?:[^\n]*\n){0,30}? matches 0 to 30 lines delimited with a line feed (\x0A) character, but as few as possible. – Wiktor Stribiżew Commented Mar 27 at 14:57
  • I think what's left that is confusing me if why the \n is repeated twice. Do understand that the or | it gave me is just doing it in both directions – Falderol Commented Mar 27 at 15:04
  • [^\n]* - the line, \n - linebreak. – Wiktor Stribiżew Commented Mar 27 at 15:07
 |  Show 5 more comments

2 Answers 2

Reset to default 1

This pattern worked for me in my editor to look for an if global_var: statement that includes text.check().

REGEX SEARCH PATTERN (PCRE2 regex flavor):

if\s+global_var:(\n   [\w+.]+\(\)){0,30}\n   test\.check\(\)

Regex demo: https://regex101/r/KANYWr/5

TEST STRING:

if global_var:
   fizz()
   test.buzz()
   bang()
   bop()

if global_var:
   fizz()
   bang()
   bop()

fizz()
test.buzz()
test.check()
bang()
bop()

if global_var:
   fizz()
   test.buzz()
   test.check()
   bang()
   bop()

if global_var:
   test.check()
   fizz()
   test.buzz()
   bang()
   bop()

MATCHES:

# MATCH 1
if global_var:
   fizz()
   test.buzz()
   test.check()

# MATCH 2
if global_var:
   test.check()

REGEX NOTES:

  • if Match literal "if".
  • \s+ Match 1 or more (+) white spaces characters, \s. Could be replaced with a single space, " ".
  • global_var: Match literal "global_var:".
  • ( Begin group.
    • \n Match newline.
    • Match three literal spaces, " ".
    • [\w+.]+ Match any alphanumerical, underscore _ or literal dot . characters 1 ore more times (+).
    • \(\) Match literal "()".
  • ){0,30} Close group. Match group 0 to 30 times {0, 30}
  • \n Match newline.
  • Match three literal spaces " ".
  • test\.check\(\) Match literal "text.check()".

Here is a way using the BOL.

`(?m)^if.*\R+(?:^[\t ]+.*\R+)*?^[\t ]+test\.check\(\).*\R?`

https://regex101/r/WR9JtI/1

(?m)
^ if .* \R+    
(?: ^ [\t ]+ .* \R+ )*?
^ [\t ]+ test \. check \( \) .* \R? 

本文标签: Can you make a regex to match two statements if they occur within N lines of each otherStack Overflow