Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
 … an ABNF Parser Generator
multiline.py
Go to the documentation of this file.
1 ''' @file examples/exp/multiline.py
2 @brief Demonstrates mimicking the multi-line mode flag of regex.
3 '''
4 import sys
5 import os
6 import re
7 # add the current working directory to the path
8 # DO NOT MOVE THE FOLLOWING STATEMENT
9 # if using autopep8 formatter, for example, set argument '--ignore=E402'
10 sys.path.append(os.getcwd())
11 from apg_py.exp.exp import ApgExp
12 
13 title = '''This example will demonstrate how to mimick
14 the multi-line mode flag of regex.
15 '''
16 print()
17 print(title)
18 
19 pattern = 'line = beg "The " animal " in the hat." end\n'
20 pattern += 'beg = (&&line-end / %^) ; begin of line or preceeded by line end\n'
21 pattern += 'end = (&line-end / %$) ; followed by line end or input\n'
22 pattern += 'animal = "cat" / "dog" / "bird" / "mouse"\n'
23 pattern += 'line-end = %d13.10 / %d10 / %d13\n'
24 print()
25 input = 'The cat in the hat.\n'
26 input += 'The dog in the hat.\n'
27 input += 'The bird in the hat.\n'
28 input += 'The mouse in the hat.'
29 header = 'RESULT'
30 testno = 0
31 
32 # Python regex
33 re_pattern = r'^The (cat|dog|bird|mouse) in the hat.$'
34 matches = re.finditer(re_pattern, input, re.M)
35 testno += 1
36 print(str(testno) + ') Python regex with the multi-line flag, re.M, set.')
37 print('PATTERN: ', end='')
38 print(re_pattern)
39 print('input string:\n' + input)
40 print()
41 print(header)
42 for match in matches:
43  print(match)
44 
45 
46 # ApgExp
47 exp = ApgExp(pattern, 'g')
48 result = exp.exec(input)
49 testno += 1
50 print('\n' + str(testno) + ') The ApgExp global match.')
51 print('PATTERN')
52 print(pattern)
53 print('input string:\n' + input)
54 print()
55 print(header)
56 while(result):
57  print(result.match)
58  result = exp.exec(input)
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.