How I can find matches in this example

hello please help, here are the raw data:

str: = "fsdfsdfs {% randstr%} dfbdfbdf {% randstr%} gdfgdfg {% randstr%} fsd {% randstr%}"

how do i find and get count the number of {% randstr%} such matches?

but how can I extract data from brackets right away, I need:
-to count how many elements are found

  • extract data inside {% …%}

I think you can use strings.Count.

count := strings.Count( str, “{% randstr%}”)

1 Like

ок, and how extract data inside {% …%} ?))

You need to find the token ({%…%} using any of Index function and then take its content. Something like

len := len(token)
for pos := strings.Index(str, token); pos != -1 ; pos = strings.Index(str, token) {
item := strings.TrimSpace(str[pos:pos+len]))
str = strings.TrimSpace(str[pos+len:])
fmt.Println(item)
}

1 Like

I need something like this, but not working((
https://play.golang.org/p/SOk6BTkno5Y

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.