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
replace.py
Go to the documentation of this file.
1
''' @file examples/exp/replace.py
2
@brief Demonstrates use of the replace 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
12
title =
'''This example will demonstrate how replace matched
13
results. The replacements can be simple string replacements,
14
replacements with components of the result or with a custom
15
function for complex replacements.
16
'''
17
print()
18
print(title)
19
20
pattern =
'''start = X / Y
21
X = A B C
22
A = "a"
23
B = "b"
24
C = "c"
25
Y = 'xyz'
26
'''
27
print()
28
print(
'PATTERN'
)
29
print(pattern)
30
header =
'RESULT'
31
testno = 0
32
33
# replace only the first match
34
exp =
ApgExp
(pattern)
35
input =
'---abc___xyz+++'
36
repl =
'555'
37
result = exp.replace(input, repl)
38
testno += 1
39
print(
'\n'
+ str(testno) +
') Replace only the first match.'
)
40
print(
'input string: '
+ input)
41
print(
'replacement string: '
+ repl)
42
print(header)
43
print(result)
44
45
# replace all matches
46
exp =
ApgExp
(pattern,
'g'
)
47
input =
'---abc___xyz+++'
48
result = exp.replace(input, repl)
49
testno += 1
50
print(
'\n'
+ str(testno) +
') Replace all matches with the "g" flag.'
)
51
print(
'input string: '
+ input)
52
print(
'replacement string: '
+ repl)
53
print(header)
54
print(result)
55
56
# special replacement characters
57
exp =
ApgExp
(pattern)
58
input =
'---abc___xyz+++'
59
repl =
'$$-$&->555-$$'
60
result = exp.replace(input, repl)
61
testno += 1
62
print(
'\n'
+ str(testno) +
') Special characters $$ and $&.'
)
63
print(
'input string: '
+ input)
64
print(
'replacement string: '
+ repl)
65
print(header)
66
print(result)
67
repl =
'left($`)|555|right($\')'
68
result = exp.replace(input, repl)
69
testno += 1
70
print(
'\n'
+ str(testno) +
') Special characters $` and $\'.'
)
71
print(
'input string: '
+ input)
72
print(
'replacement string: '
+ repl)
73
print(header)
74
print(result)
75
76
# rule name matches in replacement
77
exp.include()
78
repl =
'${C}-${B}-${A}'
79
result = exp.replace(input, repl)
80
testno += 1
81
print(
'\n'
+ str(testno) +
') Special characters ${rulename}.'
)
82
print(
'input string: '
+ input)
83
print(
'replacement string: '
+ repl)
84
print(header)
85
print(result)
86
87
88
def
fn
(input, result):
89
# note rule names are case insensitive and always stored in lower case
90
if(
len
(result.rules[
'A'
.lower()])):
91
special =
'555'
92
else
:
93
special =
'666'
94
return
special
95
96
97
# complex replacement function
98
exp =
ApgExp
(pattern,
'g'
)
99
exp.include()
100
result = exp.replace(input, fn)
101
testno += 1
102
print(
'\n'
+ str(testno) +
') Special function for complex replacement.'
)
103
print(
'input string: '
+ input)
104
print(header)
105
print(result)
106
107
testno += 1
108
print(
109
'\n'
+
110
str(testno) +
111
') See examples/exp/ast.py for an example of using the AST for a complex replacement function.'
)
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
examples.basics.substrings.len
int len
Definition:
substrings.py:27
examples.exp.replace.fn
def fn(input, result)
Definition:
replace.py:88
Generated by
1.9.1
Python APG, Version 1.0, is licensed under the
2-Clause BSD License
,
an
Open Source Initiative
Approved License.