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

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

Go regexp find word with apostrophe

问题

我正在尝试找到两个单词之间的子字符串,但是我的起始单词包含一个撇号,我似乎无法匹配它。

例如,在以下句子中:

  1. bus driver drove steady although the bus's steering was going nuts.

我的搜索的正确答案应该是:

  1. steering was going nuts

而不是:

  1. driver ... nuts

我尝试了这个:

  1. re := regexp.MustCompile("(?s)bus[\\'].*?nuts")

我还尝试了这个:

  1. 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

  1. bus driver drove steady although the bus's steering was going nuts.

the correct answer to my search should be:

  1. steering was going nuts

and not:

  1. driver ... nuts

I tried this

  1. re := regexp.MustCompile("(?s)bus[\\\'].*?nuts")

I also tried this:

  1. re := regexp.MustCompile("(?s)bus'.*?nuts")

Can't seem to make it work.

答案1

得分: 2

正确答案应该是"steering was going nuts"...

如果你想要这个子字符串作为匹配结果,你应该相应地调整你的正则表达式。

  1. re := regexp.MustCompile(`(?s)bus's (.*?nuts)`)
  2. rm := re.FindStringSubmatch(str)
  3. if len(rm) != 0 {
  4. fmt.Printf("%q\n", rm[0]) // "bus's steering was going nuts"
  5. fmt.Printf("%q", rm[1]) // "steering was going nuts"
  6. }

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.

  1. re := regexp.MustCompile("(?s)bus's (.*?nuts)")
  2. rm := re.FindStringSubmatch(str)
  3. if len(rm) != 0 {
  4. fmt.Printf("%q\n", rm[0]) // "bus's steering was going nuts"
  5. fmt.Printf("%q", rm[1]) // "steering was going nuts"
  6. }

<kbd>GoPlay</kbd>

答案2

得分: 0

您可以使用字符串字面量(使用反引号)来包含单引号,并使用捕获组:

  1. re := regexp.MustCompile(`(?s)bus'.\s+(.*?nuts)`)

参考这个示例

  1. var source_txt = `bus driver drove steady although the bus's steering was going nuts.`
  2. func main() {
  3. fmt.Printf("Experiment with regular expressions.\n")
  4. fmt.Printf("source text:\n")
  5. fmt.Println("--------------------------------")
  6. fmt.Printf("%s\n", source_txt)
  7. fmt.Println("--------------------------------")
  8. // a regular expression
  9. regex := regexp.MustCompile(`(?s)bus'.\s+(.*?nuts)`)
  10. fmt.Printf("regex: '%v'\n", regex)
  11. matches := regex.FindStringSubmatch(source_txt)
  12. for i, v := range matches {
  13. fmt.Printf("match %2d: '%s'\n", i+1, v)
  14. }
  15. }

输出:

  1. Experiment with regular expressions.
  2. source text:
  3. --------------------------------
  4. bus driver drove steady although the bus's steering was going nuts.
  5. --------------------------------
  6. regex: '(?s)bus'.\s+(.*?nuts)'
  7. match 1: 'bus's steering was going nuts'
  8. 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:

  1. re := regexp.MustCompile(`(?s)bus&#39;.\s+(.*?nuts)`)

See this example:

  1. var source_txt = `bus driver drove steady although the bus&#39;s steering was going nuts.`
  2. func main() {
  3. fmt.Printf(&quot;Experiment with regular expressions.\n&quot;)
  4. fmt.Printf(&quot;source text:\n&quot;)
  5. fmt.Println(&quot;--------------------------------&quot;)
  6. fmt.Printf(&quot;%s\n&quot;, source_txt)
  7. fmt.Println(&quot;--------------------------------&quot;)
  8. // a regular expression
  9. regex := regexp.MustCompile(`(?s)bus&#39;.\s+(.*?nuts)`)
  10. fmt.Printf(&quot;regex: &#39;%v&#39;\n&quot;, regex)
  11. matches := regex.FindStringSubmatch(source_txt)
  12. for i, v := range matches {
  13. fmt.Printf(&quot;match %2d: &#39;%s&#39;\n&quot;, i+1, v)
  14. }
  15. }

Output:

  1. Experiment with regular expressions.
  2. source text:
  3. --------------------------------
  4. bus driver drove steady although the bus&#39;s steering was going nuts.
  5. --------------------------------
  6. regex: &#39;(?s)bus&#39;.\s+(.*?nuts)&#39;
  7. match 1: &#39;bus&#39;s steering was going nuts&#39;
  8. 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:

确定