@grain/stdlib 0.4.0 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/regex.md ADDED
@@ -0,0 +1,449 @@
1
+ ---
2
+ title: Regex
3
+ ---
4
+
5
+ Regular Expressions.
6
+
7
+ <details disabled>
8
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
9
+ No other changes yet.
10
+ </details>
11
+
12
+ ```grain
13
+ import Regex from "regex"
14
+ ```
15
+
16
+ ## Values
17
+
18
+ Functions for working with regular expressions.
19
+
20
+ ### Regex.**make**
21
+
22
+ <details disabled>
23
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
24
+ No other changes yet.
25
+ </details>
26
+
27
+ ```grain
28
+ make : String -> Result<RegularExpression, String>
29
+ ```
30
+
31
+ Compiles the given pattern string into a regular expression object.
32
+
33
+ For a general overview of regular expressions, refer to
34
+ ["Mastering Regular Expressions"](http://regex.info/book.html) by Friedl, or other online resources.
35
+
36
+ Regular expressions are a combination of normal and special characters. A normal
37
+ character in a pattern will match a one-character string containing that character.
38
+ Moreover, if there are two regular expressions `A` and `B`, they can be concatenated
39
+ into a regular expression `AB`. If a string `p` matches `A` and `q` matches `B`,
40
+ then `pq` will match `AB`.
41
+
42
+ The special character sequences are as follows:
43
+
44
+ - `.` - Matches any character, except for a newline in multi-line mode
45
+ - `^` - Matches the beginning of the input, or after a newline (`\n`) in multi-line mode
46
+ - `$` - Matches the end of the input, or right before a newline (`\n`) in multi-line mode
47
+ - `«re»*` - Matches `«re»` zero or more times
48
+ - `«re»+` - Matches `«re»` one or more times
49
+ - `«re»?` - Matches `«re»` zero or one times
50
+ - `«re»{«n»}` - Matches `«re»` exactly `«n»` times
51
+ - `«re»{«n»,}` - Matches `«re»` `«n»` or more times
52
+ - `«re»{,«m»}` - Matches `«re»` zero to `«m»` times
53
+ - `«re»{«n»,«m»}` - Matches `«re»` between `«n»` and `«m»` times
54
+ - `«re»{}` - Matches `«re»` zero or more times
55
+ - `[«rng»]` - Matches any character in `«rng»` (see below)
56
+ - `[^«rng»]` - Matches any character not in `«rng»` (see below)
57
+ - `\«n»` - Matches the latest match for group `«n»` (one-indexed)
58
+ - `\b` - Matches the boundary of `\w*` (`\w` defined below, under "basic classes")
59
+ - `\B` - Matches where `\b` does not
60
+ - `\p{«property»}` - Matches any character with Unicode property `«property»` (see below)
61
+ - `\P{«property»}` - Matches any character without Unicode property `«property»` (see below)
62
+ - `(«re»)` - Matches `«re»`, storing the result in a group
63
+ - `(?:«re»)` - Matches `«re»` without storing the result in a group
64
+ - `(?«mode»:«re») - Matches `«re»` with the mode settings specified by `«mode»` using the following syntax:
65
+ - `«mode»i` - The same as `«mode»`, but with case-insensitivity enabled (temporarily not supported until grain-lang/grain#661 is resolved)
66
+ - `«mode»-i` - The same as `«mode»`, but with case-insensitivity disabled (the default)
67
+ - `«mode»m` / `«mode»-s` - The same as `«mode»`, but with multi-line mode enabled
68
+ - `«mode»-m` / `«mode»s` - The same as `«mode»`, but with multi-line mode disabled
69
+ - An empty string, which will not change any mode settings
70
+ - `(?«tst»«re1»|«re2»)` - Will match `«re1»` if `«tst»`, otherwise will match `«re2»`. The following options are available for `«tst»`
71
+ - `(«n»)` - Will be true if group `«n»` has a match
72
+ - `(?=«re»)` - Will be true if `«re»` matches the next sequence
73
+ - `(?!«re»)` - Will be true if `«re»` does not match the next sequence
74
+ - `(?<=«re»)` - Will be true if `«re»` matches the preceding sequence
75
+ - `(?<!«re»)` - Will be true if `«re»` does not match the preceding sequence
76
+ - `(?«tst»«re»)` - Equivalent to `(?«tst»«re»|)`
77
+ - Finally, basic classes (defined below) can also appear outside of character ranges.
78
+
79
+ Character ranges (referred to as `«rng»` above) have the following syntax:
80
+ - `«c»` - Matches the character `«c»` exactly
81
+ - `«c1»-«c2»` - Matches any character with a character code between the character code for `«c1»` and the code for `«c2»`
82
+
83
+ These forms can be repeated any number of times, which will construct a range of their union. That is, `[ba-c]` and `[a-c]` are equivalent ranges.
84
+ Additionally, there are the following special cases:
85
+ - A `]` as the first character of the range will match a `]`
86
+ - A `-` as the first or last character of the range will match a `-`
87
+ - A `^` in any position other than the first position will match a `^`
88
+ - `\«c»`, where `«c»` is a non-alphabetic character, will match `«c»`
89
+
90
+ Furthermore, ranges can include character classes, which are predefined commonly-used
91
+ sets of characters. There are two "flavors" of these: *basic* classes and *POSIX* classes.
92
+ Both are provided for ease of use and to maximize compatibility with other regular
93
+ expression engines, so feel free to use whichever is most convenient.
94
+
95
+ The *basic* classes are as follows:
96
+ - `\d` - Matches `0-9`
97
+ - `\D` - Matches characters not in `\d`
98
+ - `\w` - Matches `a-z`, `A-Z`, `0-9`, and `_`
99
+ - `\W` - Matches characters not in `\w`
100
+ - `\s` - Matches space, tab, formfeed, and return
101
+ - `\S` - Matches characters not in `\s`
102
+ The *POSIX* classes are as follows:
103
+ - `[:alpha:]` - Matches `a-z` and `A-Z`
104
+ - `[:upper:]` - Matches `A-Z`
105
+ - `[:lower:]` - Matches `a-z`
106
+ - `[:digit:]` - Matches `0-9`
107
+ - `[:xdigit:]` - Matches `0-9`, `a-f`, and `A-F`
108
+ - `[:alnum:]` - Matches `a-z`, `A-Z`, and `0-9`
109
+ - `[:word:]` - Matches `a-z`, `A-Z`, `0-9`, and `_`
110
+ - `[:blank:]` - Matches space and tab
111
+ - `[:space:]` - Matches space, tab, newline, formfeed, and return
112
+ - `[:cntrl:]` - Contains all characters with code points < 32
113
+ - `[:ascii:]` - Contains all ASCII characters
114
+
115
+ Parameters:
116
+
117
+ |param|type|description|
118
+ |-----|----|-----------|
119
+ |`regexString`|`String`|The regular expression to compile|
120
+
121
+ Returns:
122
+
123
+ |type|description|
124
+ |----|-----------|
125
+ |`Result<RegularExpression, String>`|The compiled regular expression|
126
+
127
+ Examples:
128
+
129
+ ```grain
130
+ Regex.make("(foo|bar)[0-9]+")
131
+ ```
132
+
133
+ ### Regex.**MatchResult**
134
+
135
+ ```grain
136
+ record MatchResult {
137
+ group: Number -> Option<String>,
138
+ groupPosition: Number -> Option<(Number, Number)>,
139
+ numGroups: Number,
140
+ allGroups: () -> Array<Option<String>>,
141
+ allGroupPositions: () -> Array<Option<(Number, Number)>>,
142
+ }
143
+ ```
144
+
145
+ This object contains the results
146
+ of a regular expression match. The results can be obtained using
147
+ the following accessors:
148
+
149
+ ```grain
150
+ group : Number -> Option<String>
151
+ ```
152
+
153
+ Returns the contents of the given group. Note that group 0 contains
154
+ the entire matched substring, and group 1 contains the first parenthesized group.
155
+
156
+ ```grain
157
+ groupPosition : Number -> Option<(Number, Number)>
158
+ ```
159
+
160
+ Returns the position of the given group.
161
+
162
+ ```grain
163
+ numGroups : Number
164
+ ```
165
+
166
+ The number of defined groups in this match object (including group 0).
167
+
168
+ ```grain
169
+ allGroups : () -> Array<Option<String>>
170
+ ```
171
+
172
+ Returns the contents of all groups matched in this match object.
173
+
174
+ ```grain
175
+ allGroupPositions : () -> Array<Option<(Number, Number)>>
176
+ ```
177
+
178
+ Returns the positions of all groups matched in this match object.
179
+
180
+ ### Regex.**isMatch**
181
+
182
+ <details disabled>
183
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
184
+ No other changes yet.
185
+ </details>
186
+
187
+ ```grain
188
+ isMatch : (RegularExpression, String) -> Bool
189
+ ```
190
+
191
+ Determines if the given regular expression has a match in the given string.
192
+
193
+ Parameters:
194
+
195
+ |param|type|description|
196
+ |-----|----|-----------|
197
+ |`rx`|`RegularExpression`|The regular expression to search for|
198
+ |`string`|`String`|The string to search within|
199
+
200
+ Returns:
201
+
202
+ |type|description|
203
+ |----|-----------|
204
+ |`Bool`|`true` if the RegExp matches the string, otherwise `false`|
205
+
206
+ Examples:
207
+
208
+ ```grain
209
+ assert Regex.isMatch(Result.unwrap(Regex.make("ca+[at]")), "caaat") == true
210
+ ```
211
+
212
+ ### Regex.**isMatchRange**
213
+
214
+ <details disabled>
215
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
216
+ No other changes yet.
217
+ </details>
218
+
219
+ ```grain
220
+ isMatchRange : (RegularExpression, String, Number, Number) -> Bool
221
+ ```
222
+
223
+ Determines if the given regular expression has a match in the given string between the given start/end offsets.
224
+
225
+ Parameters:
226
+
227
+ |param|type|description|
228
+ |-----|----|-----------|
229
+ |`rx`|`RegularExpression`|The regular expression to search for|
230
+ |`string`|`String`|The string to search|
231
+ |`start`|`Number`|The start offset to search between|
232
+ |`end`|`Number`|The end offset to search between|
233
+
234
+ Returns:
235
+
236
+ |type|description|
237
+ |----|-----------|
238
+ |`Bool`|`true` if the RegExp matches the string in the given range, otherwise `false`|
239
+
240
+ Examples:
241
+
242
+ ```grain
243
+ assert Regex.isMatchRange(Result.unwrap(Regex.make("ca+[at]")), "caaat", 0, 5) == true
244
+ ```
245
+
246
+ ```grain
247
+ assert Regex.isMatchRange(Result.unwrap(Regex.make("ca+[at]")), "caaat", 1, 5) == false
248
+ ```
249
+
250
+ ### Regex.**find**
251
+
252
+ <details disabled>
253
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
254
+ No other changes yet.
255
+ </details>
256
+
257
+ ```grain
258
+ find : (RegularExpression, String) -> Option<MatchResult>
259
+ ```
260
+
261
+ Returns the first match for the given regular expression contained within the given string.
262
+
263
+ Parameters:
264
+
265
+ |param|type|description|
266
+ |-----|----|-----------|
267
+ |`rx`|`RegularExpression`|The regular expression to search for|
268
+ |`string`|`String`|The string to search|
269
+
270
+ Returns:
271
+
272
+ |type|description|
273
+ |----|-----------|
274
+ |`Option<MatchResult>`|The match result, if any|
275
+
276
+ Examples:
277
+
278
+ ```grain
279
+ Regex.find(Result.unwrap(Regex.make("ca+[at]")), "caaat")
280
+ ```
281
+
282
+ ### Regex.**findRange**
283
+
284
+ <details disabled>
285
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
286
+ No other changes yet.
287
+ </details>
288
+
289
+ ```grain
290
+ findRange :
291
+ (RegularExpression, String, Number, Number) -> Option<MatchResult>
292
+ ```
293
+
294
+ Returns the first match for the given regular expression contained within the given string
295
+ between the given start/end range.
296
+
297
+ Parameters:
298
+
299
+ |param|type|description|
300
+ |-----|----|-----------|
301
+ |`rx`|`RegularExpression`|The regular expression to search for|
302
+ |`string`|`String`|The string to search|
303
+ |`start`|`Number`|The start offset to search between|
304
+ |`end`|`Number`|The end offset to search between|
305
+
306
+ Returns:
307
+
308
+ |type|description|
309
+ |----|-----------|
310
+ |`Option<MatchResult>`|The match result, if any|
311
+
312
+ Examples:
313
+
314
+ ```grain
315
+ Regex.findRange(Result.unwrap(Regex.make("ca+[at]")), "caaat", 0, 5)
316
+ ```
317
+
318
+ ### Regex.**findAll**
319
+
320
+ ```grain
321
+ findAll : (RegularExpression, String) -> List<MatchResult>
322
+ ```
323
+
324
+ Returns all matches for the given regular expression contained within the given string.
325
+
326
+ Parameters:
327
+
328
+ |param|type|description|
329
+ |-----|----|-----------|
330
+ |`rx`|`RegularExpression`|The regular expression to search for|
331
+ |`string`|`String`|The string to search|
332
+
333
+ Returns:
334
+
335
+ |type|description|
336
+ |----|-----------|
337
+ |`List<MatchResult>`|The list of matches|
338
+
339
+ ### Regex.**findAllRange**
340
+
341
+ <details disabled>
342
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
343
+ No other changes yet.
344
+ </details>
345
+
346
+ ```grain
347
+ findAllRange :
348
+ (RegularExpression, String, Number, Number) -> List<MatchResult>
349
+ ```
350
+
351
+ Returns all matches for the given regular expression contained within the given string
352
+ between the given start/end range.
353
+
354
+ Parameters:
355
+
356
+ |param|type|description|
357
+ |-----|----|-----------|
358
+ |`rx`|`RegularExpression`|The regular expression to search for|
359
+ |`string`|`String`|The string to search|
360
+ |`start`|`Number`|The start offset to search between|
361
+ |`end`|`Number`|The end offset to search between|
362
+
363
+ Returns:
364
+
365
+ |type|description|
366
+ |----|-----------|
367
+ |`List<MatchResult>`|The list of matches|
368
+
369
+ Examples:
370
+
371
+ ```grain
372
+ Regex.findAllRange(Result.unwrap(Regex.make("ca+[at]")), "caaat", 0, 5)
373
+ ```
374
+
375
+ ### Regex.**replace**
376
+
377
+ <details disabled>
378
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
379
+ No other changes yet.
380
+ </details>
381
+
382
+ ```grain
383
+ replace : (RegularExpression, String, String) -> String
384
+ ```
385
+
386
+ Replaces the first match for the given regular expression contained within the given string with the specified replacement.
387
+ Replacement strings support the following syntax:
388
+ - `$&` - Replaced with the text of the matching portion of input (e.g. for `(foo)`, the search string `foo bar`, and the replacement `baz $&`, the result will be `baz foo bar`)
389
+ - `$n` / `$nn` (where `n` is a digit) - Replaced with the text of group `nn`
390
+ - `$$` - Replaced with a literal `$`
391
+ - `$.` - Does nothing (this exists to support replacement strings such as `$4$.0`, which will place the contents of group 4 prior to a zero)
392
+ - `$\`` - Replaced with the text preceding the matched substring
393
+ - `$'` - Replaced with the text following the matched substring
394
+ - Any other character will be placed as-is in the replaced output.
395
+
396
+ Parameters:
397
+
398
+ |param|type|description|
399
+ |-----|----|-----------|
400
+ |`rx`|`RegularExpression`|The regular expression to search for|
401
+ |`toSearch`|`String`|The string to search|
402
+ |`replacement`|`String`|The string that replaces matches|
403
+
404
+ Returns:
405
+
406
+ |type|description|
407
+ |----|-----------|
408
+ |`String`|The given string with the appropriate replacements, if any|
409
+
410
+ Examples:
411
+
412
+ ```grain
413
+ assert Regex.replace(Result.unwrap(Regex.make("o")), "foo", "a") == "fao"
414
+ ```
415
+
416
+ ### Regex.**replaceAll**
417
+
418
+ <details disabled>
419
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
420
+ No other changes yet.
421
+ </details>
422
+
423
+ ```grain
424
+ replaceAll : (RegularExpression, String, String) -> String
425
+ ```
426
+
427
+ Replaces all matches for the given regular expression contained within the given string with the specified replacement.
428
+ See `replace` for replacement string syntax.
429
+
430
+ Parameters:
431
+
432
+ |param|type|description|
433
+ |-----|----|-----------|
434
+ |`rx`|`RegularExpression`|The regular expression to search for|
435
+ |`toSearch`|`String`|The string to search|
436
+ |`replacement`|`String`|The string that replaces matches|
437
+
438
+ Returns:
439
+
440
+ |type|description|
441
+ |----|-----------|
442
+ |`String`|The input string with the appropriate replacements, if any|
443
+
444
+ Examples:
445
+
446
+ ```grain
447
+ assert Regex.replaceAll(Result.unwrap(Regex.make("o")), "skoot", "r") == "skrrt"
448
+ ```
449
+