gpt4 book ai didi

ruby - 在文本中捕获所有大写字母 "phrase"

转载 作者:数据小太阳 更新时间:2023-10-29 08:13:24 24 4
gpt4 key购买 nike

我试图从一个长文本中提取一系列全部大写的单词。因此,在示例中:

Here is a couple words of text. If you want more information please reference the DOCUMENT RECORDS log. It explains the PATH TO ENLIGHTENMENT book that will solve all problems. Also, don't forget the CAR TUNE-UP GUIDELINES.

我想捕获文档记录启蒙之路汽车 retrofit 指南

以下正则表达式似乎在 Rubular 中有效(除了它们不匹配包含连字符的情况)。

(\b([A-Z]|-|\s){3,}\b)    
(\b[A-Z- ]{3,}\b)
(\b[A-Z-\s]{3,}\b)

但是,我无法让它们在我的程序中工作。当我在我的程序中运行以下代码时(这是一个 rake 任务),

if content.to_s.scan(/\b[A-Z-\s]{3,}\b/)
item_title = content.to_s.scan(/\b[A-Z-\s]{3,}\b/)

我收到以下错误:

... Long stack trace followed by:

[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html

如果我将 \s 替换为一个空格,我不会收到错误,但它似乎不再匹配任何内容。我正在运行 Ruby 1.9.3。为什么它在 rubular.com 网站上有效,但在我的程序中却无效?我意识到我的正则表达式很草率。我怎样才能改进它以使其更精确?

最佳答案

不了解 Ruby,但您所描述的可以用以下方法完成:

/\b[A-Z-]+(?:\s+[A-Z-]+)+\b/

关于ruby - 在文本中捕获所有大写字母 "phrase",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9374250/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com