-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticle-regex.html
More file actions
executable file
·396 lines (362 loc) · 21.8 KB
/
article-regex.html
File metadata and controls
executable file
·396 lines (362 loc) · 21.8 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<!DOCTYPE html>
<html lang="en">
<head>
<!--__RIM_REDIRECT__-->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@300..700&display=swap" rel="stylesheet">
<title>Using Regex in RimStone</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="HandheldFriendly" content="True"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="canonical" href="https://rimstone-lang.com/article-regex.html" />
<style>
body, html {
font-family: Quicksand,monospace,Helvetica, Arial, sans-serif;
line-height: 150%;
font-size:16px;
}
body {
padding-left:1vw;
padding-right:1vw;
}
.ncode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.shcode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.sqlcode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.htmlcode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.code {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
/*Just like h1 but for pdf conversion it would be indented this way it's not*/
.vhub {
display: block;
font-size: 1.6em;
margin-top: 0.63em;
margin-bottom: 0.63em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
/*Just like h2 but for pdf conversion it would be indented this way it's not*/
.vsub {
display: block;
font-size: 1.25em;
margin-top: 0.53em;
margin-bottom: 0.53em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
ul {
margin-left: 0.75vw;
padding-left: 0;
}
li {
margin-left: 0.75vw;
padding-left: 0;
}
/* this must be last, as it overrides previous settings, for mobile */
@media (hover: none) {
a {
display: inline-block;
padding-top: 3px;
padding-bottom: 2px;
}
body {
padding-left:2vw;
padding-right:2vw;
letter-spacing: 1px;
}
}
/*The following is for code snippets that are highlighted by 2html vim*/
pre { overflow-x: scroll; margin:0; padding:0; font-family:monospace; }
.Identifier { color: #008b8b; }
.Statement { color: #af5f00; }
.PreProc { color: #5fd7ff; }
.Type { color: #005f00; }
.Comment { color: blue ; }
.Constant { color: #ff00ff; }
/*end of highlighted snippets*/
a {
text-decoration:none;
padding-bottom: 0px;
color:inherit;
border-bottom: 2px solid #6cb8f0;
}
a:hover {
text-decoration: none;
color:black;
border-bottom: 1px solid red;
}
/*do not underline links nor should they be active*/
pre a {
text-decoration:none;
color:black;
border-bottom: none;
pointer-events: none;
cursor: default;
}
.golfSnippet {display:none;}
ul {
list-style-type:square;
list-style-position: outside;
}
</style>
</head>
<body>
<div id="google_translate_element" style='float:right'></div>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<script>
function rim_copy(gt, eid, gc) {
gt.textContent = eid.textContent;
gt.select();
document.execCommand("copy");
gc.style.visibility="visible"
setTimeout(()=>{ gc.style.visibility="hidden"; }, 1000);
}
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element');
}
</script>
<!--RIMMENU13-->
<!--RIMENDMENU13-->
<!--BEGVDOC90-->
<div class='vhub' style='margin-top:10px;margin-right:20px;text-align:left;background-color:white;'><a href='https://rimstone-lang.com' style='border-bottom:0px'><img src='https://rimstone-lang.com/rimstone.png' style='width:180px;height:auto'/></a></div><div class='vhub' style='margin-top:10px;'>Using Regex in RimStone</div><hr/><br/>
Regex (or "REGular EXpressions") is a powerful way to search strings, as well as to replace parts of strings. It makes it easier to reliably do so, since regular expressions are usually short; at the same time there's a learning curve in becoming proficient. However, given that regex is ubiquitous, it is worth learning at least some of it, as it can come handy.<br/>
<br/>
RimStone's implementation of regex is via <a href='https://rimstone-lang.com/match-regex.html'>match-regex</a> statement, which allows for both searching and replacing in a single statement. It also has a caching capability (meaning caching of compiled regex process), which can increase performance by up to 500%.<br/>
<br/>
This article will show a few basic ways to use match-regex statement in your RimStone code.<br/>
<br/>
Create directory for your application:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_306' class=notranslate>
<span class="Statement">mkdir</span> <span class="Special">-p</span> regex
<span class="Statement">cd</span> regex</pre>
<span id=rimstone_copied_306 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_306' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_306, code_306, rimstone_copied_306)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Create "reg" application:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_309' class=notranslate>
rim <span class="Special">-k</span> reg</pre>
<span id=rimstone_copied_309 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_309' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_309, code_309, rimstone_copied_309)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Create file "reg.rim" with this:<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_305' class=notranslate>
cat << '<span class="Constant">EOF</span>' > reg.rim
<span class="Statement">%%</span> /reg<span class="Identifier"> public</span>
<span class="Comment">// Use backreferences to swap two words, with </span><span class="Constant">"Reverse order word"</span><span class="Comment"> as a result</span>
<span class="Statement"> match-regex</span> <span class="Constant">"(word)</span><span class="Special">\\</span><span class="Constant">s+(order)"</span><span class="Identifier"> in</span> <span class="Constant">"Reverse word order"</span><span class="Identifier"> replace-with</span> <span class="Constant">"</span><span class="Special">\\</span><span class="Constant">2 </span><span class="Special">\\</span><span class="Constant">1"</span><span class="Type"> result</span> res
<span class="Statement"> print-out</span> res<span class="Identifier"> new-line</span>
<span class="Comment">// Recognize a pattern, in this case </span><span class="Constant">3</span><span class="Comment"> found</span>
<span class="Statement"> match-regex</span> <span class="Constant">"[abc]{3}"</span><span class="Identifier"> in</span> <span class="Constant">"Recognize 'aaa' or 'aa' or 'abc' or 'cab'"</span><span class="Type"> status</span> st
<span class="Statement"> print-out</span> st<span class="Identifier"> new-line</span>
<span class="Comment">// Recognize a pattern, in this case not found</span>
<span class="Statement"> match-regex</span> <span class="Constant">"[abc]{3}"</span><span class="Identifier"> in</span> <span class="Constant">"Recognize 'aa' or 'aa' or 'bc' or 'ca'"</span><span class="Type"> status</span> st
<span class="Statement"> print-out</span> st<span class="Identifier"> new-line</span>
<span class="Comment">// Use case insensitive search to recognize a pattern, in this case </span><span class="Constant">3</span><span class="Comment"> found</span>
<span class="Statement"> match-regex</span> <span class="Constant">"[abc]{3}"</span><span class="Identifier"> in</span> <span class="Constant">"Recognize 'aAa' or 'aa' or 'aBc' or 'Cab'"</span><span class="Identifier"> case-insensitive</span><span class="Type"> status</span> st
<span class="Statement"> print-out</span> st<span class="Identifier"> new-line</span>
<span class="Comment">// Use case insensitive search to recognize a pattern and replace, with </span><span class="Constant">"Recognize 'XXX' or 'aa' or 'XXX' or 'XXX'"</span><span class="Comment"> as a result</span>
<span class="Statement"> match-regex</span> <span class="Constant">"[abc]{3}"</span><span class="Identifier"> in</span> <span class="Constant">"Recognize 'aAa' or 'aa' or 'aBc' or 'Cab'"</span><span class="Identifier"> replace-with</span> <span class="Constant">"XXX"</span><span class="Type"> result</span> res<span class="Identifier"> case-insensitive</span><span class="Type"> status</span> st
<span class="Statement"> print-out</span> res<span class="Identifier"> new-line</span>
<span class="Statement"> print-out</span> st<span class="Identifier"> new-line</span>
<span class="Statement">%%</span>
<span class="Constant">EOF</span></pre>
<span id=rimstone_copied_305 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_305' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_305, code_305, rimstone_copied_305)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Build your application server as a native executable:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_311' class=notranslate>
rim <span class="Special">-q</span></pre>
<span id=rimstone_copied_311 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_311' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_311, code_311, rimstone_copied_311)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Run it from the command line:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_313' class=notranslate>
rim <span class="Special">-r</span> <span class="Special">--req</span><span class="Statement">=</span><span class="Statement">"</span><span class="Constant">/reg</span><span class="Statement">"</span> <span class="Special">--exec</span> <span class="Special">--silent-header</span></pre>
<span id=rimstone_copied_313 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_313' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_313, code_313, rimstone_copied_313)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
The result is:<br/>
<div class='ncode' style='position:relative;padding-right:16px;'>
<pre id='code_304' class=notranslate>
Reverse order word
3
0
3
Recognize 'XXX' or 'aa' or 'XXX' or 'XXX'
3</pre>
<span id=rimstone_copied_304 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_304' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_304, code_304, rimstone_copied_304)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Let's go over this one by one. <br/>
<div class="vsub"><a id="Backreferences"></a>Backreferences</div>
The first statement uses back-references, which is a way to refer to something that's found in the string:<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_308' class=notranslate>
<span class="Comment">// Use backreferences to swap two words</span>
<span class="Statement">match-regex</span> <span class="Constant">"(word)</span><span class="Special">\\</span><span class="Constant">s+(order)"</span><span class="Identifier"> in</span> <span class="Constant">"Reverse word order"</span><span class="Identifier"> replace-with</span> <span class="Constant">"</span><span class="Special">\\</span><span class="Constant">2 </span><span class="Special">\\</span><span class="Constant">1"</span><span class="Type"> result</span> res
<span class="Statement">print-out</span> res<span class="Identifier"> new-line</span></pre>
<span id=rimstone_copied_308 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_308' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_308, code_308, rimstone_copied_308)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Here, "word" and "order" are found. Since they are in parenthesis (meaning within "()"), they can be used as back-references. The first one would be \1, the second one \2 etc. In "replace-with" clause, we refer to them as "\\1" and "\\2" just because backslash in a special character used to escape others and needs to be escaped itself. "\\s+" means find any spaces ("\\s") which repeat at least one time ("+"). So we're looking for essentially a snippet like "word order" or "word order", and we are then replacing that with "\\2 \\1". Keep in mind that "\\1" refers to "word" and "\\2" refers to "order". So the result will be "order word", i.e. the two words will be output in reverse order.<br/>
<div class="vsub"><a id="Finding pattern, and counting them"></a>Finding pattern, and counting them</div>
A common use of regex is to find out if a pattern is showing up in a string, and how many times. Consider this:<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_310' class=notranslate>
<span class="Comment">// Recognize a pattern, found</span>
<span class="Statement">match-regex</span> <span class="Constant">"[abc]{3}"</span><span class="Identifier"> in</span> <span class="Constant">"Recognize 'aaa' or 'aa' or 'abc' or 'cab'"</span><span class="Type"> status</span> st
<span class="Statement">print-out</span> st<span class="Identifier"> new-line</span></pre>
<span id=rimstone_copied_310 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_310' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_310, code_310, rimstone_copied_310)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Here, you're looking for any of the characters "a", "b" or "c" that repeat 3 times (which is what "{3}" does). Obviously "aaa", "abc" and "cab" fit that bill, while "aa" does not, so the output is 3.<br/>
<br/>
Conversely, in this case, there are no instances of 3 characters (with each being "a", "b" or "c"), since all of them of length 2 (such as "aa", "bc" etc.), so the result will be 0:<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_312' class=notranslate>
<span class="Comment">// Recognize a pattern, in this case not found</span>
<span class="Statement">match-regex</span> <span class="Constant">"[abc]{3}"</span><span class="Identifier"> in</span> <span class="Constant">"Recognize 'aa' or 'aa' or 'bc' or 'ca'"</span><span class="Type"> status</span> st
<span class="Statement">print-out</span> st<span class="Identifier"> new-line</span></pre>
<span id=rimstone_copied_312 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_312' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_312, code_312, rimstone_copied_312)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
<div class="vsub"><a id="Case insensitive search"></a>Case insensitive search</div>
By default, the search is case sensitive. You can make it case insensitive with "case-insensitive" clause:<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_314' class=notranslate>
<span class="Comment">// Use case insensitive search to recognize a pattern</span>
<span class="Statement">match-regex</span> <span class="Constant">"[abc]{3}"</span><span class="Identifier"> in</span> <span class="Constant">"Recognize 'aAa' or 'aa' or 'aBc' or 'Cab'"</span><span class="Identifier"> case-insensitive</span><span class="Type"> status</span> st
<span class="Statement">print-out</span> st<span class="Identifier"> new-line</span></pre>
<span id=rimstone_copied_314 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_314' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_314, code_314, rimstone_copied_314)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
In this case, there are also 3 matches ("aAa", "aBc" and "Cab").<br/>
<div class="vsub"><a id="Search and replace"></a>Search and replace</div>
In the following example, we search for a pattern and replace it with something:<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_315' class=notranslate>
<span class="Comment">// Use case insensitive search to recognize a pattern and replace</span>
<span class="Statement">match-regex</span> <span class="Constant">"[abc]{3}"</span><span class="Identifier"> in</span> <span class="Constant">"Recognize 'aAa' or 'aa' or 'aBc' or 'Cab'"</span><span class="Identifier"> replace-with</span> <span class="Constant">"XXX"</span><span class="Type"> result</span> res<span class="Identifier"> case-insensitive</span><span class="Type"> status</span> st
<span class="Statement">print-out</span> res<span class="Identifier"> new-line</span>
<span class="Statement">print-out</span> st<span class="Identifier"> new-line</span></pre>
<span id=rimstone_copied_315 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_315' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_315, code_315, rimstone_copied_315)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Just like in previous example, 3 patterns will be recognized and replaced with "XXX" and with 3 matches as the status, the result is:<br/>
<div class='ncode' style='position:relative;padding-right:16px;'>
<pre id='code_307' class=notranslate>
Recognize 'XXX' or 'aa' or 'XXX' or 'XXX'
3</pre>
<span id=rimstone_copied_307 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_307' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_307, code_307, rimstone_copied_307)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
<div class="vsub"><a id="Lookahead and lookbehind"></a>Lookahead and lookbehind</div>
Some times you'd like to search for a pattern, but only if there's another pattern before it ("lookbehind") or after it ("lookahead").<br/>
<!--ENDVDOC90-->
<br/><div style='width:100%;clear:both;'>
<hr/>
<!--RIMFOOT77--><span style='font-size:80%'><a href="https://rimstone-lang.com/copyright.html">Copyright</a> (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.</span>
</div><br/></body></html>