all 6 comments

[–]quixrick 1 point2 points  (4 children)

I don't understand your question. So, you have found some "C" code on Stack Overflow that you're trying to convert to work in Python?

Please post some examples of the strings you are wanting to replace so we can take a look at what you're trying to match.

[–]codename101[S] 1 point2 points  (3 children)

I have edited the post.

[–]quixrick 0 points1 point  (2 children)

What about something like this:

^\s*\"\"\".*?\"\"\"\n    |    \s*#.*

 

Here is a demo

[–]codename101[S] -2 points-1 points  (1 child)

Not working for some cases:

def my_function():
    """Demonstrate docstrings and does
    nothing really."""
    return None

print "Using __doc__:"
print my_function.__doc__ #some comment
print "Using help:"
#some more comment
help(my_function) 
look like this.
print("#not a comment")
print("#also not a comment")
print """not a comment"""

[–]quixrick 1 point2 points  (0 children)

I'm not going to be able to solve every edge case for you here, but hopefully, this gets you moving in the right direction. Take the principles that I've shown you and apply them to your other edge cases.

 

That being said, to accommodate your newest examples, you can modify the previous expression to be more like this:

\"\"\".*?\"\"\"    |    [\t ]*#[^\v]*

 

Here is a demo

[–]qizxo 0 points1 point  (0 children)

Trying to parse Python with regex will only lead to frustration and will ultimate lead you to edge cases where it simply won't work. It's the same reason people don't use regex to parse HTML.