按字段内容包含字符串进行筛选

huangapple go评论75阅读模式
英文:

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)
}

huangapple
  • 本文由 发表于 2017年1月14日 02:50:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/41641501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定