Hello,
I hope it won't be a garbage post, because I'm missing something super simple...
I'm sending a Json from a Python script to a server written in Nim. The Json data is between START and END strings.
if "START" in buf:
buildingRecord = true
if buildingRecord:
dataBuf.add(buf)
if "END" in buf and buildingRecord:
# Extract the message.
var matches: seq[string]
var matched = find(dataBuf, re"START((.|\s)*)END", matches)
if matched > 0:
echo(matches[0])
The regex finds no matches. I hope it's not a problem with the regex itself. I've tested it online and it seems to work. The Json looks like so:
{
"start_h":10,
"start_m":40,
"end_h":11,
"end_m":40,
"delta_h":0,
"delta_m":0,
"project_ID":0
}
It gets wrapped between START and END and pushed through a socket to the server. Server gets the data for sure. It just fails to extract the data with regex.
With some fixes:
import re, strutils
let buf = """
START
{
"start_h":10,
"start_m":40,
"end_h":11,
"end_m":40,
"delta_h":0,
"delta_m":0,
"project_ID":0
}
END
"""
var
dataBuf: string = ""
buildingRecord: bool
if "START" in buf:
buildingRecord = true
if buildingRecord:
dataBuf.add(buf)
if "END" in buf and buildingRecord:
# Extract the message.
var matches: array[1, string]
var matched = find(dataBuf, re(r"START(.+)END", flags={reDotAll}), matches)
if matched >= 0:
# Or just:
# if contains(dataBuf, re(r"START(.+)END", flags={reDotAll}), matches):
echo(matches[0])
var matches: array[1, string]
Why is
var matches: seq[string]
wrong? I thought that's a list of strings that can be dynamically extended.
bumping this in case you are still wondering after 1 year...
the answer is that using seq does not work in re.match and you have to know beforehand the exact length of the matches array. This is an acknowledge bug (which nre does not have): https://github.com/nim-lang/Nim/issues/9472
(see also this SO answer)