vlang

V simple regex use


The following code compiles and runs in V 0.4.0 049d685 without a problem but seems to give incorrect results. I must be doing something wrong, but what is it?

Thanks in advance.

import regex

fn main() {
  txt := 'Now is the time for all good men'
  query := r'the'
  mut re := regex.regex_opt(query) or {panic(err)}
  println(re.get_query())           //-> the
  println(re.matches_string(txt)    //-> false
  start,end := re.match_string(txt)
  if start >= 0 {
    println('Match: ${txt[start..end]} between ${start} and ${end}')
  }
  else {println('No match')}        //-> No match
}

I was expecting a match to 'the'.


Solution

  • matches_string matches on the whole string. For instance, when using query := r'.*the.*' your code will match "the", and output true.

    As @doneforaiur pointed out, you could use println(re.find_all_str(txt)) instead of println(re.matches_string(txt)), which would output ['the'].