Skip to content

IlanZuckerman/python_regex_parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Intro:
The program matches a given regex pattern and prints out the matches according different arguments which are passed in cmd.
The program can parse a list of given files OR a given string. Meaning that if user did'nt pass -f argument (file paths),
he will be prompted to enter a string which will be later used for matching the pattern.
Both files "test.txt" and "test2.txt" can be used for testing and exploring the program. You can pass them as -f values comma separated.
For help about usage of arguments, type "python2.7 parse_input_by_regex.py -h"

Compatibility: Python >= 2.6

Needed modules: colorama

Exception handling:
    - If a given file name does not exist on file system, an informative error will be shown, and the program will proceed to next file (if given).
    - If a user provided any of those args (-m, -u, -c, -h) without -f arg (which specifies files for parse), an error will be printed telling
      the user that any of those args should come in conjunction with -h.
      The logic behind this decision is that it does not make sense to print a machine format (for example) if no files where specified.

List of arguments:
  -p              regex pattern. Required. Syntax: "(<pattern>)" for example: -p "(h.)"   -p "(a[bd])"
  -f              files paths. comma delimited. Optional. Default is None
  -m              machine format. Optional. Default is False
  -u              prints ^ under the matching text. Optional. Default is False
  -c              highlight matching text. Optional. Default is False
  -hu             display matches in human readable format. Optional. Default is False

If argument -f was not passed, then the program will ask the user to type a string on which the pattern will be matched,
and list of matched strings will be printed out.
IMPORTANT to note that in this case, you should not pass any additional arguments (except -p) to avoid error messages.

The following arguments can be passed one after another with specified files: -hu -c -u -m
For example: python2.7 parse_input_by_regex.py -p "(\d{4,5})" -f "test.txt" -hu -c -u -m
In this case, the file content matches will be presented in all 4 formats (human, color, underline, machine)


Usage examples:

This example will highlight all the matches in a given file along with a humanly readable format giving you all the needed info.
>>> python2.7 parse_input_by_regex.py -p "(\d{4,5})" -f "test.txt" -hu -c
File name: test.txt Found on line 1: 62543
File name: test.txt Found on line 1: 5437
File name: test.txt Found on line 2: 3725
File name: test.txt Found on line 3: 76543
62543653aba5437
abadshgfaba3725
765432
sdfghjklkjhg


This example prints out underscored matches for both given files. '*' line means that second file is being parsed

>>> python2.7 parse_input_by_regex.py -p "(\d{4,5})" -f "test.txt,test2.txt" -u
62543653aba5437
^^^^^
abadshgfaba3725
           ^^^^
765432
^^^^^
sdfghjklkjhg
************************************************
sdfsf
sada12345
    ^^^^^

This example will print out the matches from both file in humanly readable format
>>> python2.7 parse_input_by_regex.py -p "(\d{4,5})" -f "test.txt,test2.txt" -hu
File name: test.txt Found on line 1: 62543
File name: test.txt Found on line 1: 5437
File name: test.txt Found on line 2: 3725
File name: test.txt Found on line 3: 76543
************************************************
File name: test2.txt Found on line 2: 12345

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages