Search code examples
regexnim-lang

nim re, regex modul is not filling match group


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?


Solution

  • 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.