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 | Show 5 more comments2 Answers
Reset to default 1This 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.[\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.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 other? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744081940a2530409.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
^if global_var:$\n(.*\n){0,30}?.*test\.check\(\)
– DuesserBaest Commented Mar 27 at 14:31(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(?:[^\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\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