[Tutor] receiving regular expression from command line

anish singh anish198519851985 at gmail.com
Sat Oct 1 04:16:56 EDT 2016


I am trying to implement grep to just increase my knowledge
about regular expression.

Below is the program usage:
python test.py -i Documents/linux/linux/ -s '\w+_readalarm*'

However, due to my lack of knowledge about string handling
in python, I am getting wrong results.

def read_file(file, pattern):
   with open(file, 'r') as outfile:
     for line in outfile:
       match = re.compile(str(pattern)).match(line)
       if match:
         print(file + " " + match.group())

Can someone let me know how can I pass regular expression
from command line?

Whole code: http://ideone.com/KxLJP2

Any other comments about the code are most welcome.


More information about the Tutor mailing list