我如何在Go中进行不区分大小写的正则表达式匹配?

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

How do I do a case insensitive regular expression in Go?

问题

我可以编写正则表达式来处理这两种情况,例如regexp.Compile("[a-zA-Z]"),但是我的正则表达式是从用户提供的字符串构建的:

reg, err := regexp.Compile(strings.Replace(s.Name, " ", "[ \\._-]", -1))

其中s.Name是名称。它可能是类似于'North by Northwest'的东西。现在,对我来说最明显的解决方案是遍历s.Name的每个字符,并为每个字母写入'[nN]':

    for i := 0; i < len(s.Name); i++ {
      if s.Name[i] == " " {
        fmt.Fprintf(str, "%s[ \\._-]", str);
      } else {
        fmt.Fprintf(str, "%s[%s%s]", str, strings.ToLower(s.Name[i]), strings.ToUpper(s.Name[i]))
      }
    }

但我觉得这是一个不太优雅的解决方案。速度并不是真正的问题,但我需要知道是否还有其他方法。

英文:

I could write my regular expression to handle both cases, such as regexp.Compile(&quot;[a-zA-Z]&quot;), but my regular expression is constructed from a string given by the user:

reg, err := regexp.Compile(strings.Replace(s.Name, &quot; &quot;, &quot;[ \\._-]&quot;, -1))

Where s.Name is the name. Which could be something like 'North by Northwest'. Now, the most apparent solution to me would be to walk through each character of s.Name and write '[nN]' for each letter:

    for i := 0; i &lt; len(s.Name); i++ {
      if s.Name[i] == &quot; &quot; {
        fmt.Fprintf(str, &quot;%s[ \\._-]&quot;, str);
      } else {
        fmt.Fprintf(str, &quot;%s[%s%s]&quot;, str, strings.ToLower(s.Name[i]), strings.ToUpper(s.Name[i]))
      }
    }

But I feel this is a rather non-elegant solution. Speed is not really a concern, but I need to know if there is another way.

答案1

得分: 251

你可以在正则表达式中设置一个不区分大小写的标志作为第一个项目。

通过在正则表达式的开头添加"(?i)"来实现。

reg, err := regexp.Compile("(?i)"+strings.Replace(s.Name, " ", "[ \\._-]", -1))

对于一个固定的正则表达式,它会像这样。

r := regexp.MustCompile(`(?i)CaSe`)

有关标志的更多信息,请搜索regexp/syntax包文档(或syntax文档)中的术语"flags"。

英文:

You can set a case-insensitive flag as the first item in the regex.

You do this by adding &quot;(?i)&quot; to the beginning of a regex.

reg, err := regexp.Compile(&quot;(?i)&quot;+strings.Replace(s.Name, &quot; &quot;, &quot;[ \\._-]&quot;, -1))

For a fixed regex it would look like this.

r := regexp.MustCompile(`(?i)CaSe`)

For more information about flags, search the
regexp/syntax package documentation
(or the syntax documentation)
for the term "flags".

答案2

得分: 44

你可以在模式的开头添加(?i)来使其不区分大小写。

参考

英文:

You can add a (?i) at the beginning of the pattern to make it case insensitive.

Reference

答案3

得分: 11

我对Go不太熟悉,但根据这个例子:http://play.golang.org/p/WgpNhwWWuW

你需要在正则表达式语句前加上(?i)

英文:

I'm not too familiar with Go, but according to this example: http://play.golang.org/p/WgpNhwWWuW

You need to prefix your regex statement with (?i)

答案4

得分: 5

使用i标志。引用提示文档

分组:

(re)           编号捕获组
(?P<name>re)   命名和编号的捕获组
(?:re)         非捕获组
(?flags)       在当前组内设置标志;非捕获
(?flags:re)    在re期间设置标志;非捕获

标志语法为xyz(设置)或-xyz(清除)或xy-z(设置xy,清除z)。标志包括:

i              不区分大小写(默认为false)
m              多行模式:^和$匹配行的开头/结尾,除了文本的开头/结尾(默认为false)
s              让.匹配\n(默认为false)
U              非贪婪模式:交换x*和x*?,x+和x+?等的含义(默认为false)
英文:

Use the i flag. Quoting the tip documentation:

> Grouping:

(re)           numbered capturing group
(?P&lt;name&gt;re)   named &amp; numbered capturing group
(?:re)         non-capturing group
(?flags)       set flags within current group; non-capturing
(?flags:re)    set flags during re; non-capturing

> Flag syntax is xyz (set) or -xyz (clear) or xy-z (set xy, clear z). The flags are:

i              case-insensitive (default false)
m              multi-line mode: ^ and $ match begin/end line in addition to begin/end text (default false)
s              let . match \n (default false)
U              ungreedy: swap meaning of x* and x*?, x+ and x+?, etc (default false)

huangapple
  • 本文由 发表于 2013年3月11日 03:19:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/15326421.html
匿名

发表评论

匿名网友

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

确定