3

I have a file with a list of regex patterns and string containing '\n's' i.e a string with seperate lines ... I need a generic regex that can match the whole line with patterns in the regex file such that i can do something like

re.compile(r'generic_regex%s') %regex_pattern from file and it automatically matches the whole line much like grep.

Any ideas??

1
  • Do you mean you want to match the entire string containing the newlines (\n), or just match the text between newlines? i.e., search in MULTILINE mode, or search each 'line' individually in the input string? Commented Aug 5, 2012 at 17:34

2 Answers 2

8

Something like:

>>> re.findall(r"(^.*?%s.*?$)" %expression, text, re.MULTILINE)

?

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

Comments

0

Adjust for any boundaries etc...

import re
import mmap

def find_re(fname, rx): # rx is a compiled re object
    with open(fname) as fin:
        mm = mmap.mmap(fin.fileno(), 0, access=mmap.ACCESS_READ)
        return rx.findall(mm)

Shoud be fast for just sequential access... re-work the regex if needs be to go over multiple lines...

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.