Re: Regex extracting string between two words not working
Thank you. That works. Could you explain to me what was wrong with my code? It seems that the important part is the var matches: array[1, string] Run Why is var matches: seq[string] Run wrong? I thought that's a list of strings that can be dynamically extended.
Re: Regex extracting string between two words not working
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]) Run
Regex extracting string between two words not working
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]) Run 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 } Run 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.