Reputation: 461
I'm trying to grab the multi-line comment in some c code that is above an arbitrary function using a regular expression. ONLY the comment immediately above the function and the function code itself is what i'm interested in. The comment will definitely have a "\abc" in it at the end (see snippet). I also don't really care if there is some more code or text below the function. A solution that contains some text/noise after the function is also acceptable.
I was thinking to grab the comment above the function via lazy evaluation but it's not quite working yet.
Here is my minimal example
import re
snippet = """
/*=================================================*
* THIS IS NOT THE COMMENT I WANT
*===============================================*/
/* THIS IS THE COMMENT I WANT.
* It should be able to have special characters like /,*.
* \\abc
*/
TEST(foo,bar){
...
}
"""
pattern = re.compile(r"(\/\*.)?\\abc.*", re.DOTALL)
search = pattern.search(snippet)
match = search.group(0)
print(match)
Output
\abc
*/
TEST(foo,bar){
...
}
Desired output
/* THIS IS THE COMMENT I WANT.
* It should be able to have special characters like /,*.
* \abc
*/
TEST(foo,bar){
...
}
Upvotes: 1
Views: 59
Reputation: 522
/\*((?!\*/).)+\\abc.*
This isn't the most readable but it works. It reads : /*
then \abc
then everything, but you can't match */
until you've matched the \abc
. Or more accurately, /*
then a number of characters that aren't */
, then \abc
, then everything.
https://regex101.com/r/x1bXVg/3
Upvotes: 3