Looking at a simple problem - find the indentation of text in a line and see if
it's a comment. Nim version:
import nre
import strutils
let commentChars = {'#', ';'}
let nonspace = re"(\S)"
let line = " ; abc "
var m = line.find(nonspace)
if isSome[RegexMatch](m):
let theMatch = get[RegexMatch](m)
let isComment = theMatch.captures[0][0] in commentChars
let pos = theMatch.captureBounds[0].get().a
echo "$# $#" % [$pos, $isComment]
Python version:
import re
comment_chars = {'#', ';'}
nonspace = re.compile(r"(\S)")
line = " ; abc "
m = nonspace.search(line)
if m:
is_comment = m.groups()[0] in comment_chars
pos = m.start(0)
print("%s %s" % (pos, is_comment))
Not hugely different, yet the Nim version just seems harder to grok. Am I using
`nre` suboptimally?