Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
… an
A
BNF
P
arser
G
enerator
Main Page
Related Pages
Packages
Packages
Package Functions
All
a
b
c
d
e
f
g
h
i
k
l
m
n
p
r
s
t
u
v
x
Functions
a
b
c
d
e
f
g
h
i
k
l
m
n
p
r
s
t
u
v
x
Variables
a
b
c
d
e
f
g
h
i
k
l
m
n
p
r
s
t
u
v
Classes
Class List
Class Index
Class Members
All
_
a
b
c
d
e
f
g
h
i
l
m
n
o
p
r
s
t
u
w
Functions
_
a
c
d
e
f
g
h
i
o
p
r
s
t
u
w
Variables
a
b
c
e
f
g
h
i
l
m
n
o
p
r
s
t
u
Files
File List
•
All
Classes
Namespaces
Files
Functions
Variables
Pages
examples
exp
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)
apg_py.exp.exp.ApgExp
The ApgExp class provides a pattern-matching engine similar to JavaScript's RegExp
Definition:
exp.py:79
apg_py.exp.exp
Definition:
exp.py:1
Generated by
1.9.1
Python APG, Version 1.0, is licensed under the
2-Clause BSD License
,
an
Open Source Initiative
Approved License.