Hi,
Example that doesn't work:
import pegs
var matches = newSeq[string]() # --> Works if using for example: newSeq[string](10)
let
fixedString = "cd C:\\something\\sometimes\\someone"
result = fixedString.match(peg"cd\s+{\D}\:", matches)
if fixedString.match(peg"cd\s+{\D}\:", matches):
echo matches [0]
How do I use pegs' "proc match(s: string; pattern: Peg; matches: var openArray[string]; start = 0): bool" without guessing the length of matches? Or is there another procedure that can be used for this?
Thanks
let
fixedString = "cd C:\\something\\sometimes\\someone"
result = fixedString.findAll(peg"cd\s+{\D}\:")
echo result
Thanks @sky_khan,
But how do I get the capture from the result? This now gives @["cd C:"], I need it to return just the captured substring {\D} ("C" in this case), like match does.
This code:
import pegs
static:
var matches = newSeq[string](10)
let
fixedString = "mkdir D:\...\nimgen_test\build\libmodbus"
if fixedString.match(peg"cd\s+{\D}\:", matches): # <-- Error in this line
echo matches[0]
throws /playground/nim/lib/pure/pegs.nim(1661, 13) Error: index 11 not in 0 .. 10. Notice the static statement.
Is this a bug?
To be honest, I've never used pegs in Nim before but looking at it's source, it has MaxSubpatterns = 20. So, I guess either you can define matches as newSeqstring or you can use this,
if fixedString =~ peg"cd\s+{\D}\:":
echo matches[0]
without defining matches at all, because it injects matches variable itself, if there is none at current scope.@sky_khan
That's it! Perfect, thanks!
Not part of the stdlib, but NPeg shoild be able to do this for you at compile time:
import npeg
static:
let p = peg rule:
rule <- "cd" * +Space * >Alpha * ":":
echo $1
discard p.match("cd c:\blue\monkey\bike")