Can I confirm if the string is matching with the first block at my regex

I’ve the below code:

package main

import (
	"fmt"
	"regexp"
)

func main() {
	str := "This is a delimited string, so let's go"
	re := regexp.MustCompile(`(?i)(This is a delimited string)|delimited|string`)
	matches := re.FindAllString(str, -1)
	fmt.Println("We found", len(matches), "matches, that are:", matches)
}

And getting the output as:

We found 1 matches, that are: [This is a delimited string]

If I changed the str in the above code to be:

str := "This is not a delimited string, so let's go"

Then I’m getting the output as:

We found 2 matches, that are: [delimited string]

Both are correct, but in the first block str which is having 1 match is matching 100% with the first block at my regex, which is This is a delimited string, while str in the second block showing 2 matches, none of them is matching with my first block at my regex.

Is there a way, so I know if the str is matching with the first block at my regex or no, so that I get complete matchorpartial matchis thelen(matches)` is not zero, but the first block at the regex is not matched!

Based on your last two questions, it looks to me like you’re creating a sort of search engine where when a user searches for something, you want to search for both:

  • An exact match (maybe after “cleaning” it up (e.g. excess whitespace, etc.)
  • Contains any of the “keywords” in the search (e.g. ignore words like “the,” “it,” “a,” etc. and search for the other words more specifically; from your example: “delimited” and “string”)

Is that right?

If so, I would just create separate regular expressions and handle their weights manually.

yes, exactly, trying to build a knowledge base with search engine, something close to the way this forum works when posting new question and showing recommended question to look into.

I solved it as playground

package main

import (
	"errors"
	"fmt"
	"regexp"
	"strings"
)

// https://golangexample.com/match-regex-group-into-go-struct-using-struct-tags-and-automatic-parsing/

func build(words ...string) (*regexp.Regexp, error) {
	// words to be excluded
	re := regexp.MustCompile(`(?i)^(this|is|a)`)

	var sb strings.Builder

	switch len(words) {
	case 0:
		return nil, errors.New("empty input")
	case 1:
		return regexp.Compile(regexp.QuoteMeta(words[0]))
	}

	quoted := make([]string, len(words))
	for i, w := range words {
		quoted[i] = regexp.QuoteMeta(w)
	}

	//sb.WriteByte(fmt.Sprintf("(?P<fullMatch>"))
	sb.WriteString(fmt.Sprintf("(?P<fullMatch>"))
	for i, w := range quoted {
		if i > 0 {
			sb.WriteByte('\x20')
		}
		sb.WriteString(w)
	}
	sb.WriteString(`)|`)
	for _, w := range quoted {
		matches := re.FindAllString(w, -1)
		if len(matches) == 0 {
			sb.WriteString(fmt.Sprintf("(%s)", w))
			sb.WriteByte('|')
		}
	}

	return regexp.Compile(`(?i)` + strings.TrimSuffix(sb.String(), "|"))
}

var words = regexp.MustCompile(`\pL+`)

func main() {

	input := "\tThis\v\x20\x20,\t\tis\t\t,?a!,¿delimited?,string‽"
	allWords := words.FindAllString(input, -1)

	re, err := build(allWords...)
	if err != nil {
		panic(err)
	}

	fmt.Println(re)

	str := "This is a delimited string, so let's go"
	//matches := re.FindAllString(str, -1)
	groupNames := re.SubexpNames()
	matches := re.FindAllStringSubmatch(str, -1)

	if len(matches) == 0 {
		fmt.Println("Sorry, no single match had been found")
	} else if len(matches[0][1]) > 0 {
		fmt.Println("An exact match had been found")
		fmt.Println("groupNames[1]:", groupNames[1], len(matches[0][1]), matches[0][1])
	} else {
		fmt.Println("A non-perfect match had been found")
		fmt.Println("We found", len(matches), "matches, that are:", matches)
	}
}

And the output for str := "This is a delimited string, so let's go" is:

(?i)(?P<fullMatch>This is a delimited string)|(delimited)|(string)
An exact match had been found
groupNames[1]: fullMatch 26 This is a delimited string

And for str := "This is not a delimited string, so let's go", the output is:

(?i)(?P<fullMatch>This is a delimited string)|(delimited)|(string)
A non-perfect match had been found
We found 2 matches, that are: [[delimited  delimited ] [string   string]]

I’ve 2 clarifications:

  1. Why in the last line, the groups are duplicated, why I have [delimited delimited ] instead of [delimited], and why I have [string string] instead of [string]

  2. Is there any comments about my proposed approach to this issue resolved?