英文:
Filter by field's content contains string
问题
我有一些对象:
{
"name": "universite paris sorbonne",
"id": "548272c9-6615-4e93-aa15-9af0a830c9a2"
}
{
"name": "universite paris dauphine",
"id": "943234f3-6615-4e93-aa15-9af0a830c9a2"
}
{
"name": "universite sorbonne nouvelle",
"id": "24f477f3-6615-4e93-aa15-9af0a830c9a2"
}
如果我想选择包含“paris dau”在其名称中的每个对象,我该怎么做?
我尝试了以下方法,但没有成功。
curs, _ = r.Table("places").
Filter(func(customer r.Term) interface{}{
return customer.Field('name').Downcase().Contains(func (custo r.Term) interface{}{
return custo.Match(".\" + strings.ToLower('paris dau') + ".\")
})
})
有什么建议吗?
英文:
I've some object :
{
"name": "universite paris sorbonne" ,
"id": "548272c9-6615-4e93-aa15-9af0a830c9a2"
}
{
"name": "universite paris dauphine" ,
"id": "943234f3-6615-4e93-aa15-9af0a830c9a2"
}
{
"name": "universite sorbonne nouvelle" ,
"id": "24f477f3-6615-4e93-aa15-9af0a830c9a2"
}
How can i do if i would like select each object which contain paris dau
in their name ?
I've tried with that, but it doesn't work.
curs, _ = r.Table("places").
Filter(func(customer r.Term) interface{}{
return customer.Field('name').Downcase().Contains(func (custo r.Term) interface{}{
return custo.Match(".\\*" + strings.ToLower('paris dau') + ".\\*")
})
any suggest ?
答案1
得分: 1
我简化了你的问题,将数据转储到一个映射中,然后使用简单的正则表达式匹配,这个方法效果很好。
package main
import (
"fmt"
"regexp"
)
func main() {
data := map[string]string{
"548272c9-6615-4e93-aa15-9af0a830c9a2": "universite paris sorbonne",
"943234f3-6615-4e93-aa15-9af0a830c9a2": "universite paris dauphine",
"24f477f3-6615-4e93-aa15-9af0a830c9a2": "universite sorbonne nouvelle",
}
var matches = make([]string, 0)
for _, value := range data {
matched, err := regexp.MatchString(".*paris dau.*", value)
if err == nil && matched {
matches = append(matches, value)
}
}
fmt.Printf("Matches: %v\n", matches)
}
以上是翻译好的代码部分。
英文:
I simplified your problem a bit by dumping the data into a map and then did a simple regexp match which works well.
package main
import (
"fmt"
"regexp"
)
func main() {
data := map[string]string{
"548272c9-6615-4e93-aa15-9af0a830c9a2": "universite paris sorbonne",
"943234f3-6615-4e93-aa15-9af0a830c9a2": "universite paris dauphine",
"24f477f3-6615-4e93-aa15-9af0a830c9a2": "universite sorbonne nouvelle",
}
var matches = make([]string, 0)
for _, value := range data {
matched, err := regexp.MatchString(".*paris dau.*", value)
if err == nil && matched {
matches = append(matches, value)
}
}
fmt.Printf("Matches: %v\n", matches)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论