Skip to content

Commit c4118ee

Browse files
authored
Merge pull request #2569 from actonlang/re-match-start-pos
Add start_pos to re.match
2 parents 1a77e7d + 4050a06 commit c4118ee

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
### Added
6+
- `re.match()` now accepts an optional `start_pos` to begin scanning at an offset.
67
- Add `acton doc` command for generating documentation [#2292]
78
- Supports multiple output formats (text, markdown, HTML)
89
- Default is to open browser into HTML docs when window environment is

base/src/re.act

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ class Match:
4343
def _match(pattern: str, string: str, start_pos: int) -> ?Match:
4444
NotImplemented
4545

46-
def match(pattern: str, string: str) -> ?Match:
46+
def match(pattern: str, string: str, start_pos: int=0) -> ?Match:
4747
"""Scan through string looking for a match to the pattern, returning
48-
a match object, or None if no match was found.
48+
a match object, or None if no match was found. start_pos sets the
49+
index to begin scanning at.
4950
"""
50-
return _match(pattern, string, 0)
51+
return _match(pattern, string, start_pos)
5152

5253
def matches(pattern: str, string: str) -> list[Match]:
5354
"""Find all non-overlapping matches in string

docs/acton-guide/src/stdlib/re.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ actor main(env):
1212
env.exit(0)
1313
```
1414

15+
`re.match` also accepts an optional `start_pos` to begin scanning at a
16+
specific index (defaults to 0).
17+
1518
Output:
1619
```sh
1720
Got a match: foobar
1821
```
19-

test/stdlib_tests/src/test_re.act

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ def _test_match_positions():
9292
# Also verify the substring matches what we expect
9393
testing.assertEqual(m.string[m.start_pos:m.end_pos], "def")
9494

95+
def _test_match_start_pos():
96+
"""Test that start_pos controls where the search begins."""
97+
s = "def def"
98+
99+
m1 = re.match("def", s)
100+
testing.assertNotNone(m1, "Should have matched first 'def'")
101+
if m1 is not None:
102+
testing.assertEqual(m1.start_pos, 0)
103+
testing.assertEqual(m1.end_pos, 3)
104+
105+
m2 = re.match("def", s, start_pos=4)
106+
testing.assertNotNone(m2, "Should have matched second 'def' starting at index 4")
107+
if m2 is not None:
108+
testing.assertEqual(m2.start_pos, 4)
109+
testing.assertEqual(m2.end_pos, 7)
110+
111+
m3 = re.match("def", s, start_pos=5)
112+
testing.assertNone(m3, "start_pos should skip earlier matches")
113+
95114
def _test_split_no_match():
96115
"""Split: pattern does not match anywhere"""
97116
res = re.split("xyz", "abc def ghi")

0 commit comments

Comments
 (0)