Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
 … an ABNF Parser Generator
basic.py
Go to the documentation of this file.
1 ''' @file examples/exp/basic.py
2 @brief Demonstrates simple matching and testing of patterns in a string.
3 '''
4 import sys
5 import os
6 # add the current working directory to the path
7 # DO NOT MOVE THE FOLLOWING STATEMENT
8 # if using autopep8 formatter, for example, set argument '--ignore=E402'
9 sys.path.append(os.getcwd())
10 from apg_py.exp.exp import ApgExp
11 
12 title = '''This example will demonstrate the basic procedure
13 for matching a pattern in a string and simply
14 testing to see if the pattern exists.
15 Note that specifying case insensitive matching is done
16 with the SABNF pattern, not with flags, as in regex.
17 '''
18 print()
19 print(title)
20 
21 pattern_ci = 'start = "abc"\n'
22 pattern_cs = 'start = %s"abc"\n'
23 input_lower = '---abc==='
24 input_upper = '---ABC==='
25 header = 'RESULT'
26 testno = 0
27 
28 # case insensitive matching
29 exp = ApgExp(pattern_ci)
30 result = exp.exec(input_lower)
31 testno += 1
32 print('\n' + str(testno) + ') Case insensitive - match the lower case pattern')
33 print('input string: ' + input_lower)
34 print(header)
35 print(result)
36 result = exp.exec(input_upper)
37 testno += 1
38 print('\n' + str(testno) + ') Case insensitive - match the upper case pattern')
39 print('input string: ' + input_upper)
40 print(header)
41 print(result)
42 
43 # case insensitive testing
44 exp = ApgExp(pattern_ci)
45 result = exp.test(input_lower)
46 testno += 1
47 print('\n' + str(testno) + ') Case insensitive - test the lower case pattern')
48 print('input string: ' + input_lower)
49 print(header)
50 print(result)
51 result = exp.test(input_upper)
52 testno += 1
53 print('\n' + str(testno) + ') Case insensitive - test the upper case pattern')
54 print('input string: ' + input_upper)
55 print(header)
56 print(result)
57 
58 # case sensitive matching
59 exp = ApgExp(pattern_cs)
60 result = exp.exec(input_lower)
61 testno += 1
62 print('\n' + str(testno) + ') Case sensitive - match the lower case pattern')
63 print('input string: ' + input_lower)
64 print(header)
65 print(result)
66 exp = ApgExp(pattern_cs)
67 result = exp.exec(input_upper)
68 testno += 1
69 print('\n' + str(testno) + ') Case sensitive - match the upper case pattern')
70 print('input string: ' + input_upper)
71 print(header)
72 print(result)
73 
74 # case sensitive testing
75 exp = ApgExp(pattern_cs)
76 result = exp.test(input_lower)
77 testno += 1
78 print('\n' + str(testno) + ') Case sensitive - test the lower case pattern')
79 print('input string: ' + input_lower)
80 print(header)
81 print(result)
82 result = exp.test(input_upper)
83 testno += 1
84 print('\n' + str(testno) + ') Case sensitive - test the upper case pattern')
85 print('input string: ' + input_upper)
86 print(header)
87 print(result)
The ApgExp class provides a pattern-matching engine similar to JavaScript's RegExp
Definition: exp.py:79
Python APG, Version 1.0, is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.