bitpshr
bitpshr

Reputation: 1063

Python-issued grep regex, match this pattern

I can't figure out how to match this pattern via a grep command I am issuing via Python.

I want to match a string in the form of

foo.bar([anything including newlines, spaces, tabs]) .

I am trying with:

regex = " foo.bar(.*) "
bashCommand = "grep"+" -r -h"+regex+baseDir
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
requires = process.communicate()[0]

But I fail to match this string

dojo.require("abc.def"


    );

Upvotes: 2

Views: 407

Answers (2)

Jacek Przemieniecki
Jacek Przemieniecki

Reputation: 5967

Grep works line-by-line, so "." does not actually match newlines. You may find this answer helpful.

Upvotes: 1

Jared Forsyth
Jared Forsyth

Reputation: 13172

By default python's "re" module has "." match everything except newline. You want to pass re.DOTALL as a flag to your regex.

Example:

rx = re.compile('foo\.bar\(.*\)', re.DOTALL)
assert rx.match('foo.bar("mystuff"\n\nand here!)')

http://docs.python.org/library/re.html#re.S

Upvotes: 0

Related Questions