3

I want to use the regex module of the nim library:

import re

var s="""<webSettings>
<add key="MyLaborPassword" value="shadowed" />
<add key="MyLaborUserID" value="shadowed" />
<add key="MyLaborUrl" value="shadowed" />
<add key="DebugSoapLoggingEnabled" value="false" />
  </webSettings>
 """


var matches : seq[string] = @[]

echo s.find(re"""MyLaborP(ass)word""",matches)
echo matches

Gives me

25
@[]

but i except:

25
@["ass"]

what have i missed?

Grzegorz Adam Hankiewicz
  • 7,349
  • 1
  • 36
  • 78
enthus1ast
  • 2,099
  • 15
  • 22
  • 1
    `re.find` fills in the sequence (or array), it doesn't add to it; if it's an empty sequence it will remain empty. Use `var matches = newSeq[string](1)` instead (i.e. a 1-element seq) or make it an `array[1, string]`. – Reimer Behrends Nov 23 '15 at 20:44
  • Reimer Behrends, this worked! But its a little strange behavior in my opinion. – enthus1ast Nov 24 '15 at 13:36
  • 1
    Davidos Krausos, this is because `find()` takes an `openarray[string]` parameter to accommodate both seqs and arrays, but an `openarray` parameter is not resizable. – Reimer Behrends Nov 24 '15 at 15:19

1 Answers1

6

The re module is deprecated and has been a bit buggy in my experience. You can use the new nre module:

import nre, options

var s="""<webSettings>
<add key="MyLaborPassword" value="shadowed" />
<add key="MyLaborUserID" value="shadowed" />
<add key="MyLaborUrl" value="shadowed" />
<add key="DebugSoapLoggingEnabled" value="false" />
  </webSettings>
 """


echo s.find(re"""MyLaborP(ass)word""").get.captures[0]

Which prints ass.

def-
  • 5,275
  • 20
  • 18
  • i've also tried nre before, but i've missed the `options`... I had to install `options` with nimble as well. And i had to clear the nimcache before compiling. – enthus1ast Nov 23 '15 at 14:38
  • 2
    You might be on an old version of Nim. The current Nim 0.12.0 comes with the options module. Nimcache clearing is a bug that I hoped would be fixed by now. – def- Nov 23 '15 at 14:50