Hello, I've been having this error lately while using regex. This error only occurs while using Nim, because I tried this thing on different Regex testing websites and it seem to work. I'm desperate to get your help and find to find a solution. I'll leave my code below. Thank you, Luqaska.
import std/strutils
import re
proc rt*(
file: string,
replace: varargs[
array[0..1, string]
]
): string =
var temp = readFile("templates/" & file)
temp = temp.replace(re"{%[\n\r\t\0\s]?(include|INCLUDE)\s(\".+\"|\'.+\')[\n\r\t\0\s]?%}", "It works!")
for x in replace:
temp = temp.replace("$" & x[0] & ";", x[1])
return temp
Consider:
import re
let x = re"a\"b"
Which fails to compile with Error: closing " expected
This isn't a special regex syntax, but 'generalized raw string literals', and raw string literals require embedded double quotes to be doubled rather than escaped: https://nim-lang.org/docs/manual.html#lexical-analysis-raw-string-literals
import re
let x = re"a""b"
echo find("""string containing a"b""", x) # 18
You can also use a triple-quoted string literal:
temp = temp.replace(re"""{%[\n\r\t\0\s]?(include|INCLUDE)\s(\".+\"|\'.+\')[\n\r\t\0\s]?%}""", "It works!")
Where did this design for character escaping via doubling come from?
Take for instance "\\" which produces a single backslash.