Skip to content

Commit 47d769f

Browse files
authored
Berry update 're' module
1 parent 751dcb1 commit 47d769f

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

docs/Berry.md

+39-15
Original file line numberDiff line numberDiff line change
@@ -1523,40 +1523,64 @@ There are two ways to use regex, first is to call directly the module which trig
15231523
15241524
# first series are all-in-one, patterns are compiled on the fly
15251525
1526+
# Returns the list of matches, or empty list of no match
15261527
> re.search("a.*?b(z+)", "zaaaabbbccbbzzzee")
15271528
['aaaabbbccbbzzz', 'zzz']
1529+
1530+
# Returns the list of list of matches
1531+
> re.searchall('<([a-zA-Z]+)>', '<abc> yeah <xyz>')
1532+
[['<abc>', 'abc'], ['<xyz>', 'xyz']]
1533+
1534+
# Returns the list of matches, or empty list of no match; must match from the beginning of the string.
15281535
> re.match("a.*?b(z+)", "aaaabbbccbbzzzee")
15291536
['aaaabbbccbbzzz', 'zzz']
1537+
1538+
# Returns the number of chars matched instead of the entire match (saves memory)
1539+
> re.match2("a.*?b(z+)", "aaaabbbccbbzzzee")
1540+
[14, 'zzz']
1541+
1542+
# Returns the list of matches, or empty list of no match; there should not be any gaps between matches.
1543+
> re.matchall('<([a-zA-Z]+)>', '<abc> yeah <xyz>')
1544+
[['<abc>', 'abc']])
1545+
> re.matchall('<([a-zA-Z]+)>', '<abc><xyz>')
1546+
[['<abc>', 'abc'], ['<xyz>', 'xyz']]
1547+
1548+
# Returns the list of strings from split
15301549
> re.split('/', "foo/bar//baz")
15311550
['foo', 'bar', '', 'baz']
1532-
> re.searchall('<([a-zA-Z]+)>', '<abc> yeah <xyz>')
1533-
[['<abc>', 'abc'], ['<xyz>', 'xyz']]
15341551
15351552
# below are pre-compiled patterns, which is much faster if you use the
15361553
# pattern multiple times
1554+
#
1555+
# the compiled pattern is a `bytes()` object that can be used
1556+
# as a replacement for the pattern string
1557+
> rb = re.compilebytes('<([a-zA-Z]+)>')
1558+
# rb is compiled to bytes('1A0000000C0000000100000062030260FB7E00013C7E020302617A415A62F87E03013E7E017F')
15371559
1538-
> rr = re.compile('<([a-zA-Z]+)>')
1539-
> rr.searchall('<abc> yeah <xyz>')
1560+
> re.searchall(rb, '<abc> yeah <xyz>')
15401561
[['<abc>', 'abc'], ['<xyz>', 'xyz']]
15411562
1542-
> rr = re.compile("/")
1543-
> rr
1544-
<instance: re_pattern()>
1563+
> rb = re.compilebytes("/")
1564+
> rb
1565+
bytes('0C000000070000000000000062030260FB7E00012F7E017F')
15451566
1546-
> rr.split("foo/bar//baz")
1567+
> re.split(rb, "foo/bar//baz")
15471568
['foo', 'bar', '', 'baz']
1548-
> rr.split("/b")
1569+
> re.split(rb, "/b")
15491570
['', 'b']
15501571
```
15511572

15521573
Tasmota Function|Parameters and details
15531574
:---|:---
1554-
search<a class="cmnd" id="re_search"></a>|`re.search(pattern:string, payload:string) -> list of strings`<br>Returns the list of matches, or empty list of no match
1555-
match<a class="cmnd" id="re_match"></a>|`re.match(pattern:string, payload:string) -> list of strings`<br>Returns the list of matches, or empty list of no match. The difference with `search` is that match must match from the beginning of the string.
1556-
searchall<a class="cmnd" id="re_searchall"></a>|`re.searchall(pattern:string, payload:string [, limit:string]) -> list of list of strings`<br>Returns the list of list of matches, or empty list of no match. `limit` allows to limit the number of matches.
1557-
matchall<a class="cmnd" id="re_matchall"></a>|`re.matchall(pattern:string, payload:string [, limit:string]) -> list of list of strings`<br>Returns the list of matches, or empty list of no match. The difference with `searchall` is that there should not be any gaps between matches. `limit` allows to limit the number of matches.
1558-
split<a class="cmnd" id="re_split"></a>|`re.search(pattern:string, payload:string) -> list of strings`<br>Returns the list of strings from split, or a list with a single element containing the entire string if no match
1559-
compile<a class="cmnd" id="re_compile"></a>|`re.compile(pattern:string) -> instance of <re_pattern>`<br>Compiles the regex into a reusable faster bytecode. You can then call the following methods:<br>`search()`, `match()`, `split()` similarly to the module's functions.
1575+
search<a class="cmnd" id="re_search"></a>|`re.search(pattern:string or bytes, payload:string [, offset:int]) -> list of strings`<br>Returns the list of matches, or empty list of no match
1576+
match<a class="cmnd" id="re_match"></a>|`re.match(pattern:string or bytes, payload:string [, offset:int]) -> list of strings`<br>Returns the list of matches, or empty list of no match. The difference with `search` is that match must match from the beginning of the string.<br>Takes an optional second argument offset which indicates at which character to start the in the payload (default 0).
1577+
match2<a class="cmnd" id="re_match2"></a>|`re.match2(pattern:string or bytes, payload:string [, offset:int]) -> list of strings`<br>Returns the list of matches, or empty list of no match. The difference with `match` is that the first element contains the number of matched characters instead of the matched string, which saves memory for large matches.<br>Takes an optional second argument offset which indicates at which character to start the in the payload (default 0).
1578+
searchall<a class="cmnd" id="re_searchall"></a>|`re.searchall(pattern:string or bytes, payload:string [, limit:string]) -> list of list of strings`<br>Returns the list of list of matches, or empty list of no match. `limit` allows to limit the number of matches.
1579+
matchall<a class="cmnd" id="re_matchall"></a>|`re.matchall(pattern:string or bytes, payload:string [, limit:string]) -> list of list of strings`<br>Returns the list of matches, or empty list of no match. The difference with `searchall` is that there should not be any gaps between matches. `limit` allows to limit the number of matches.
1580+
split<a class="cmnd" id="re_split"></a>|`re.search(pattern:string or bytes, payload:string) -> list of strings`<br>Returns the list of strings from split, or a list with a single element containing the entire string if no match
1581+
compilebytes<a class="cmnd" id="re_compilebytes"></a>|`re.compilebytes(pattern:string) -> instance of bytes()`<br>Compiles the regex into a reusable faster bytecode. You can then use the `bytes()` compiled pattern as a replacement for the patter string
1582+
compile<a class="cmnd" id="re_compile"></a>|**Deprecated**, use `compilebytes` instead.<br>`re.compile(pattern:string) -> instance of <re_pattern>`<br>Compiles the regex into a reusable faster bytecode. You can then call the following methods:<br>`search()`, `match()`, `split()` similarly to the module's functions.
1583+
dump<a class="cmnd" id="re_dump"></a>|`re.dump(pattern:bytes) -> nil`<br>Prints to the console a dump of the compiled pattern.<br>Only if compiled with `#define USE_BERRY_DEBUG` and only for curiosity/debugging purpose.
15601584

15611585
Note: for `match` and `search`, the first element in the list contains the global match of the pattern. Additional elements correspond to the sub-groups (in parenthesis).
15621586

0 commit comments

Comments
 (0)