summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJared Hancock <jared.hancock@centeredsolutions.com>2024-03-25 20:58:51 -0500
committerDamien George <damien@micropython.org>2025-08-11 14:11:56 +1000
commit14ccdeb4d7b9b88ab012258e77d3070340fbc3da (patch)
tree5dfddbc202170ecaa12f1d61746442afef938f5b /tests
parent485dac783b8ba7b88fdbf28fcdf54eb053cd8ef7 (diff)
extmod/modre: Add support for start- and endpos.
Pattern objects have two additional parameters for the ::search and ::match methods to define the starting and ending position of the subject within the string to be searched. This allows for searching a sub-string without creating a slice. However, one caveat of using the start-pos rather than a slice is that the start anchor (`^`) remains anchored to the beginning of the text. Signed-off-by: Jared Hancock <jared@greezybacon.me>
Diffstat (limited to 'tests')
-rw-r--r--tests/extmod/re_start_end_pos.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/extmod/re_start_end_pos.py b/tests/extmod/re_start_end_pos.py
new file mode 100644
index 000000000..bd1658437
--- /dev/null
+++ b/tests/extmod/re_start_end_pos.py
@@ -0,0 +1,78 @@
+# test start and end pos specification
+
+try:
+ import re
+except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+
+def print_groups(match):
+ print("----")
+ try:
+ if match is not None:
+ i = 0
+ while True:
+ print(match.group(i))
+ i += 1
+ except IndexError:
+ pass
+
+
+p = re.compile(r"o")
+m = p.match("dog")
+print_groups(m)
+
+m = p.match("dog", 1)
+print_groups(m)
+
+m = p.match("dog", 2)
+print_groups(m)
+
+# No match past end of input
+m = p.match("dog", 5)
+print_groups(m)
+
+m = p.match("dog", 0, 1)
+print_groups(m)
+
+# Caret only matches the actual beginning
+p = re.compile(r"^o")
+m = p.match("dog", 1)
+print_groups(m)
+
+# End at beginning means searching empty string
+p = re.compile(r"o")
+m = p.match("dog", 1, 1)
+print_groups(m)
+
+# End before the beginning doesn't match anything
+m = p.match("dog", 2, 1)
+print_groups(m)
+
+# Negative starting values don't crash
+m = p.search("dog", -2)
+print_groups(m)
+
+m = p.search("dog", -2, -5)
+print_groups(m)
+
+# Search also works
+print("--search")
+
+p = re.compile(r"o")
+m = p.search("dog")
+print_groups(m)
+
+m = p.search("dog", 1)
+print_groups(m)
+
+m = p.search("dog", 2)
+print_groups(m)
+
+# Negative starting values don't crash
+m = p.search("dog", -2)
+print_groups(m)
+
+m = p.search("dog", -2, -5)
+print_groups(m)