-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
71 lines (58 loc) · 3.35 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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