英文:
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("[a-zA-Z]")
, but my regular expression is constructed from a string given by the user:
reg, err := regexp.Compile(strings.Replace(s.Name, " ", "[ \\._-]", -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 < 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]))
}
}
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 "(?i)"
to the beginning of a regex.
reg, err := regexp.Compile("(?i)"+strings.Replace(s.Name, " ", "[ \\._-]", -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)
来使其不区分大小写。
答案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<name>re) named & 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论