-
Notifications
You must be signed in to change notification settings - Fork 0
/
subsitute-pattern.xml
46 lines (30 loc) · 1.01 KB
/
subsitute-pattern.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?xml version='1.0' encoding='UTF-8'?>
<snippets>
<!--
Created by Frescobaldi 2.18.2.
Every snippet is represented by:
title: title text
shortcuts: list of shortcut elements, every shortcut is a key sequence
body: the snippet text
The snippet id attribute can be the name of a builtin snippet or a random
name like 'n123456'. In the latter case, the title is used to determine
whether a snippet is new or updated.
-->
<snippet id="n605534">
<title>Substitute pattern</title>
<shortcuts />
<body>-*- python; indent: no;
import re
# Change the below to optional pattern and caller name
subst = "$p1 4 $p2 $p3 $p1"
caller = "\example"
# The below shouldn't need to be changed
nps = len(set(re.findall(r"\$p\d+", subst)))
esc_call = caller.replace("\\", "\\\\")
prgx = r"([a-x]+(?![A-Za-z])(?:,|')*)"
rgx = r"\s*".join([esc_call] + [prgx] * nps)
rgxsubst = re.sub(r"\$p(\d+)\s*(\d*)\s*", r"\\g<\1>\2 ", subst)
text = re.sub(rgx, rgxsubst, text)
</body>
</snippet>
</snippets>