0

I tried various regexes including the ones mentioned @ Python 3 regular expression to find multiline comment to match my input file given below,complete code is also below.Following is the regex am using currenlty to match the input file @http://pastie.org/5653293

pattern = re.compile(r'/\*.*?'+ needle + '.*?\*/', re.DOTALL)

can someone provide inputs on why the regex is not matching?

import os
import sys
import re
import fnmatch

def find_and_remove(haystack, needle):
    re.escape(needle)
    pattern = re.compile(r'/\*.*?'+ needle + '.*?\*/', re.DOTALL)
    return re.sub(pattern, "", haystack)

for path,dirs,files in os.walk(sys.argv[1]):
    for fname in files:
        for pat in ['*.cpp','*.c','*.h','*.txt']:
            if fnmatch.fnmatch(fname,pat):
                fullname = os.path.join(path,fname)
                # put all the text into f and read and replace...
                f = open(fullname).read()
                result = find_and_remove(f, r"Copyright (c) 2012, The Linux Foundation. All rights reserved")

INPUT:- http://pastie.org/5653293

9
  • You cannot parse programming languages with regexes. Consider something like bitbucket.org/eliben/pycparser Commented Jan 9, 2013 at 10:50
  • 2
    And I'm wondering what's the point of removing TLF's copyrights from sources? Commented Jan 9, 2013 at 10:52
  • @thg435 - may be it an overkill for this exercise unless you prove me wrong...I looked at the above doc..doesnt have good examples..have you used it before? Commented Jan 9, 2013 at 10:52
  • @thg435 - just removing the old 2012 comments...since its 2013 Commented Jan 9, 2013 at 10:53
  • No, I haven't used it before... I guess the api is similar to the python's ast module. You might want to ask the author for explanations and examples. Commented Jan 9, 2013 at 10:58

1 Answer 1

1

Use "Copyright \(c\) 2012, The Linux Foundation. All rights reserved". You need to escape the parenthesis since they already have a meaning (capture) in regular expressions.

Search for more info on how to escape regexes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.