Regex multiline support status #5373
Replies: 1 comment
-
This pattern is working as it should with regards to how the VM sees the string you gave it.
The VM sees that the line is a valid start of a multiline string, proceeds, dispatches the
You will not be able to get any more captures than what you specify in your pattern, per run of the VM. If you want multiple captures of the same pattern, you must either manually run the VM on different strings (slices of the original string, perhaps) or use the iterator. Note however that using the iterator with this pattern will not work either, as neither the VM nor the iterator will skip the string ahead upon a failed match. This is crucial to understand why this works the way it does. I have considered changing the iterator to make your intended pattern work, and while I think it could be done for this narrow usecase, it complicates things. For instance, what do you do if someone tries a Multiline match with no Here's an example that does what you want but is simpler: package main
import "core:fmt"
import "core:strings"
import "core:text/regex"
main :: proc() {
test := "\n\n\ntest\n// test\ntest"
rg, err := regex.create_by_user(`/^test$/`)
assert(err == nil)
defer regex.destroy_regex(rg)
caps := regex.preallocate_capture()
defer regex.destroy(caps)
line_num := 1
for line in strings.split_lines_iterator(&test) {
defer line_num += 1
groups, ok := regex.match_with_preallocated_capture(rg, line, &caps)
if !ok { continue }
fmt.printfln("% 4i %q: %v %v", line_num, line, caps.groups[:groups], caps.pos[:groups])
}
} Running this results in:
You're able to get line numbers this way: critical information for any sensible parser. I think |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi 👋
I've been playing with regex on a project, so far so good up until I tried to use it in multiline context.
And I don't know if it's me or a current issue...
Here is an example to illustrate:
With this example nothing returns.
If I remove the
^
only the first one.The goal here was, to get the first and the third occurences.
So it looks like
global
is implicit andmultiline
is ignored.I've seen that you made changes around that @Feoramund recently and if you are willing to point me to the right direction I would be very thankful.
Beta Was this translation helpful? Give feedback.
All reactions