英文:
Go regexp find word with apostrophe
问题
我正在尝试找到两个单词之间的子字符串,但是我的起始单词包含一个撇号,我似乎无法匹配它。
例如,在以下句子中:
bus driver drove steady although the bus's steering was going nuts.
我的搜索的正确答案应该是:
steering was going nuts
而不是:
driver ... nuts
我尝试了这个:
re := regexp.MustCompile("(?s)bus[\\'].*?nuts")
我还尝试了这个:
re := regexp.MustCompile("(?s)bus'.*?nuts")
似乎无法使其工作。
英文:
I am trying to find a substring between two words, but my starting word contains an apostrophe and I can't seem to match it.
For example, in this following sentence
bus driver drove steady although the bus's steering was going nuts.
the correct answer to my search should be:
steering was going nuts
and not:
driver ... nuts
I tried this
re := regexp.MustCompile("(?s)bus[\\\'].*?nuts")
I also tried this:
re := regexp.MustCompile("(?s)bus'.*?nuts")
Can't seem to make it work.
答案1
得分: 2
正确答案应该是"steering was going nuts"
...
如果你想要这个子字符串作为匹配结果,你应该相应地调整你的正则表达式。
re := regexp.MustCompile(`(?s)bus's (.*?nuts)`)
rm := re.FindStringSubmatch(str)
if len(rm) != 0 {
fmt.Printf("%q\n", rm[0]) // "bus's steering was going nuts"
fmt.Printf("%q", rm[1]) // "steering was going nuts"
}
英文:
>The correct answer to my search should be "steering was going nuts"
...
If you want that substring as your match result, you should adjust your regex accordingly.
re := regexp.MustCompile("(?s)bus's (.*?nuts)")
rm := re.FindStringSubmatch(str)
if len(rm) != 0 {
fmt.Printf("%q\n", rm[0]) // "bus's steering was going nuts"
fmt.Printf("%q", rm[1]) // "steering was going nuts"
}
<kbd>GoPlay</kbd>
答案2
得分: 0
您可以使用字符串字面量(使用反引号)来包含单引号,并使用捕获组:
re := regexp.MustCompile(`(?s)bus'.\s+(.*?nuts)`)
参考这个示例:
var source_txt = `bus driver drove steady although the bus's steering was going nuts.`
func main() {
fmt.Printf("Experiment with regular expressions.\n")
fmt.Printf("source text:\n")
fmt.Println("--------------------------------")
fmt.Printf("%s\n", source_txt)
fmt.Println("--------------------------------")
// a regular expression
regex := regexp.MustCompile(`(?s)bus'.\s+(.*?nuts)`)
fmt.Printf("regex: '%v'\n", regex)
matches := regex.FindStringSubmatch(source_txt)
for i, v := range matches {
fmt.Printf("match %2d: '%s'\n", i+1, v)
}
}
输出:
Experiment with regular expressions.
source text:
--------------------------------
bus driver drove steady although the bus's steering was going nuts.
--------------------------------
regex: '(?s)bus'.\s+(.*?nuts)'
match 1: 'bus's steering was going nuts'
match 2: 'steering was going nuts'
FindStringSubmatch()
函数用于:
在字符串
s
中找到正则表达式的最左匹配以及其子表达式的匹配(如果有的话)
match[1]
将是第一个捕获组。
英文:
You could use a string literal (with back quote) in order to include a single quote, with a capturing group:
re := regexp.MustCompile(`(?s)bus'.\s+(.*?nuts)`)
See this example:
var source_txt = `bus driver drove steady although the bus's steering was going nuts.`
func main() {
fmt.Printf("Experiment with regular expressions.\n")
fmt.Printf("source text:\n")
fmt.Println("--------------------------------")
fmt.Printf("%s\n", source_txt)
fmt.Println("--------------------------------")
// a regular expression
regex := regexp.MustCompile(`(?s)bus'.\s+(.*?nuts)`)
fmt.Printf("regex: '%v'\n", regex)
matches := regex.FindStringSubmatch(source_txt)
for i, v := range matches {
fmt.Printf("match %2d: '%s'\n", i+1, v)
}
}
Output:
Experiment with regular expressions.
source text:
--------------------------------
bus driver drove steady although the bus's steering was going nuts.
--------------------------------
regex: '(?s)bus'.\s+(.*?nuts)'
match 1: 'bus's steering was going nuts'
match 2: 'steering was going nuts'
The FindStringSubmatch()
:
> identifying the leftmost match of the regular expression in s and the matches, if any, of its subexpressions
The match[1]
would be the first capturing group.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论