使用正则表达式在文本中查找带有撇号的单词。

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

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

GoPlay

英文:

>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&#39;.\s+(.*?nuts)`)

See this example:

var source_txt = `bus driver drove steady although the bus&#39;s steering was going nuts.`

func main() {
	fmt.Printf(&quot;Experiment with regular expressions.\n&quot;)
	fmt.Printf(&quot;source text:\n&quot;)
	fmt.Println(&quot;--------------------------------&quot;)
	fmt.Printf(&quot;%s\n&quot;, source_txt)
	fmt.Println(&quot;--------------------------------&quot;)

	// a regular expression
	regex := regexp.MustCompile(`(?s)bus&#39;.\s+(.*?nuts)`)
	fmt.Printf(&quot;regex: &#39;%v&#39;\n&quot;, regex)
	matches := regex.FindStringSubmatch(source_txt)
	for i, v := range matches {
		fmt.Printf(&quot;match %2d: &#39;%s&#39;\n&quot;, i+1, v)
	}
}

Output:

Experiment with regular expressions.
source text:
--------------------------------
bus driver drove steady although the bus&#39;s steering was going nuts.
--------------------------------
regex: &#39;(?s)bus&#39;.\s+(.*?nuts)&#39;
match  1: &#39;bus&#39;s steering was going nuts&#39;
match  2: &#39;steering was going nuts&#39;

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.

huangapple
  • 本文由 发表于 2014年11月16日 22:16:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/26957907.html
匿名

发表评论

匿名网友

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

确定