Let's say I have a line in the file "This is perhaps the easiest place to add new features." and I want to lie two words close together. I do
grep -ERHn "\beasiest\W+(?:\w+\W+){1,6}?place\b" *
which works and gives me a string. But when I do
grep -ERHn "\beasiest\W+(?:\w+\W+){1,10}?new\b" *
does he fail by defeating the whole point {1,10}? This list is listed on regular-expression.info, as well as in several Regex books. Although they do not describe it with grep, it does not matter.
Update
I put regex in a python script. It works, but has nothing good grep -C ...
import re
import sys
import os
word1 = sys.argv[1]
word2 = sys.argv[2]
dist = sys.argv[3]
regex_string = (r'\b(?:'
+ word1
+ r'\W+(?:\w+\W+){0,'
+ dist
+ '}?'
+ word2
+ r'|'
+ word2
+ r'\W+(?:\w+\W+){0,'
+ dist
+ '}?'
+ word1
+ r')\b')
regex = re.compile(regex_string)
def findmatches(PATH):
for root, dirs, files in os.walk(PATH):
for filename in files:
fullpath = os.path.join(root,filename)
with open(fullpath, 'r') as f:
matches = re.findall(regex, f.read())
for m in matches:
print "File:",fullpath,"\n\t",m
if __name__ == "__main__":
findmatches(sys.argv[4])
Call
python near.py charlie winning 6 path/to/charlie/sheen
works for me.
source
share