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
split.py
Go to the documentation of this file.
1
''' @file examples/exp/split.py
2
@brief Demonstrates the use of the split() function.
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
from
apg_py.lib
import
utilities
as
utils
12
13
title =
'''This example will demonstrate how to use
14
the split function. This function is similar to the
15
JavaScript string split function
16
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)
17
with a regex expression for the split pattern.
18
All flags except the character code flag "c" are ignored.
19
If the "c" flag is set, substitute "tuple of character codes" for string.
20
- if the input string is empty, the output list contains
21
a single empty string
22
- if the pattern matches the entire string, the output list contains
23
a single empty string.
24
- if no pattern matches are found in the input string,
25
the output list contains a single string which
26
is a copy of the input string.
27
- if the pattern finds multiple matches, the output list contains
28
a each of the strings between the matches
29
- if the pattern matches the empty string, the output will be a list
30
of the single characters.
31
'''
32
print()
33
print(title)
34
35
pattern =
'start = %s"The entire string."\n'
36
pat =
'PATTERN'
37
header =
'RESULT'
38
testno = 0
39
40
# empty input string
41
exp =
ApgExp
(pattern)
42
input =
''
43
result = exp.split(input)
44
testno += 1
45
print(
'\n'
+ str(testno) +
') Empty input string.'
)
46
print(pat)
47
print(pattern)
48
print(
'input string: '
+ input)
49
print(header)
50
print(result)
51
52
# pattern matches the entire string
53
exp =
ApgExp
(pattern)
54
input =
'The entire string.'
55
result = exp.split(input)
56
testno += 1
57
print(
'\n'
+ str(testno) +
') Pattern matches the entire string.'
)
58
print(pat)
59
print(pattern)
60
print(
'input string: '
+ input)
61
print(header)
62
print(result)
63
64
# separator matches
65
pattern =
'''pattern = wsp / (owsp "," owsp) / (owsp ";" owsp)
66
wsp = %d32 / %d9
67
owsp = *wsp
68
'''
69
exp =
ApgExp
(pattern)
70
input =
'dog cat,bird ; mouse, rabbit'
71
result = exp.split(input)
72
testno += 1
73
print(
'\n'
+ str(testno) +
') Multiple separator matches.'
)
74
print(pat)
75
print(pattern)
76
print(
'input string: '
+ input)
77
print(header)
78
print(result)
79
80
# separator matches with limits
81
pattern =
'''pattern = wsp / (owsp "," owsp) / (owsp ";" owsp)
82
wsp = %d32 / %d9
83
owsp = *wsp
84
'''
85
exp =
ApgExp
(pattern)
86
input =
'dog cat,bird ; mouse, rabbit'
87
result = exp.split(input, limit=3)
88
testno += 1
89
print(
'\n'
+ str(testno) +
') Multiple separator matches limited to 3.'
)
90
print(pat)
91
print(pattern)
92
print(
'input string: '
+ input)
93
print(header)
94
print(result)
95
96
# separator matches with limits using characters
97
pattern =
'''pattern = wsp / (owsp "," owsp) / (owsp ";" owsp)
98
wsp = %d32 / %d9
99
owsp = *wsp
100
'''
101
exp =
ApgExp
(pattern,
'c'
)
102
input =
'dog cat,bird ; mouse, rabbit'
103
result = exp.split(utils.string_to_tuple(input), limit=3)
104
testno += 1
105
print(
106
'\n'
+
107
str(testno) +
108
') Multiple separator matches limited to 3 in character codes.'
)
109
print(pat)
110
print(pattern)
111
print(
'input string: '
+ input)
112
print(header)
113
print(result)
114
115
# matches the empty string
116
pattern =
'''pattern = ""
117
'''
118
exp =
ApgExp
(pattern)
119
input =
'Separate into characters.'
120
result = exp.split(input)
121
testno += 1
122
print(
'\n'
+ str(testno) +
') Separate into characters with an empty string match.'
)
123
print(pat)
124
print(pattern)
125
print(
'input string: '
+ input)
126
print(header)
127
print(result)
128
129
# matches the empty string in characters
130
pattern =
'''pattern = ""
131
'''
132
exp =
ApgExp
(pattern,
'c'
)
133
input =
'characters'
134
result = exp.split(utils.string_to_tuple(input))
135
testno += 1
136
print(
137
'\n'
+
138
str(testno) +
139
') Separate into character codes with an empty string match.'
)
140
print(pat)
141
print(pattern)
142
print(
'input string: '
+ input)
143
print(header)
144
print(result)
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
apg_py.lib
Definition:
__init__.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.