I am attempting to search a directory for a given string pattern in Python. I then want to assemble the matches into an array.
At first, I was trying to use grep:
regex = " dojo.require(..*) "
bashCommand = "grep"+" --only-matching -r -h"+regex+baseDir
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
dirStr = process.communicate()[0]
But I realized I needed to support strings on multiple lines, such as
dojo.require(
"abc"(;
so grep is not an option.
What other ways can I accomplish this? Thanks in advance.
Rather than calling grep, you could implement this functionality in pure Python using a combination of os and re. Use the re.DOTALL flag to allow multi-line matches. For example: