Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Ruby中检查单词的第一个和最后一个字符是否相同?_Ruby_String - Fatal编程技术网

如何在Ruby中检查单词的第一个和最后一个字符是否相同?

如何在Ruby中检查单词的第一个和最后一个字符是否相同?,ruby,string,Ruby,String,如果我有一个句子字符串,我想检查每个单词的第一个和最后一个字母是否相同,并找出哪些单词的第一个和最后一个字母相同。例如: sentence_one = "Label the bib numbers in red." 这将打印出以同一字母开头和结尾的所有单词(不区分大小写) 您可以使用正则表达式: sentence_one = "Label the bib numbers in red" sentence_one.scan(/(\b(\w)\w*(\2)\b)/i) #=> [["Lab

如果我有一个句子字符串,我想检查每个单词的第一个和最后一个字母是否相同,并找出哪些单词的第一个和最后一个字母相同。例如:

sentence_one = "Label the bib numbers in red."

这将打印出以同一字母开头和结尾的所有单词(不区分大小写)

您可以使用正则表达式:

sentence_one = "Label the bib numbers in red"

sentence_one.scan(/(\b(\w)\w*(\2)\b)/i)
#=> [["Label", "L", "l"], ["bib", "b", "b"]]

\b
是一个单词边界,
\w
匹配一个字母(您可能需要调整此值)。共有3个捕获:(1)整个单词,(2)第一个字母和(3)最后一个字母。使用
\2
需要最后一个字母与第一个字母匹配。

在一篇评论中,OP询问如何获得具有所需属性的单词计数。这里有一种方法。我假设所需的属性是单词的第一个字符和最后一个字符是相同的,尽管可能大小写不同。这里有一种方法可以做到这一点,它不会产生一个元素将被计数的中间数组

sentence_one = "Label the bib numbers in red"
puts sentence_one.split(' ').count{|word| word[0] == word[-1]} # => 1
r = /
    \b            # match a word break
    (?:           # begin a non-capture group
      \p{Alpha}   # match a letter
      |           # or
      (\p{Alpha}) # match a letter in capture group 1
      \p{Alpha}*  # match zero or more letters
      \1          # match the contents of capture group 1
    )             # end the non-capture group
    \b            # match a word break
    /ix           # case-indifferent and free-spacing regex definition modes

str = "How, now is that a brown cow?"

str.gsub(r).count
  #=> 2
请参见,特别是只有一个参数且未提供块的情况


当正则表达式包含捕获组时,有时使用
scan
(请参阅)。这些问题通常可以通过使用
gsub
后接
to_a
(或)来避免。

只需在数组中添加一个选项(跳过一个字母单词):


先分成几个字。然后检查第一个/最后一个字符。这两件事中的哪一件导致了问题?“找到1”-有2吗?此外,为什么字符串被包装在数组中?可以有多个字符串吗?对于给定的示例,您的预期结果是什么?无论您如何操作,第一步您可能希望删除任何标点符号:
“My,oh My!”.gsub(/[[:punct:]]/,“”)=>“My oh My”
“我想检查每个单词的第一个字母和最后一个字母是否相同。”这是什么意思?是否要返回包含具有该属性的单词的数组?有那么多字具有这种属性?显示(
放置
)每个具有该属性的单词?那个案子呢?“标签”是否具有该属性?请注意,因为您声明字符串是一个句子,所以它必须包含标点符号,至少是一个句子结束符。通常在Ruby中,您会使用
单词来完成此操作。选择
,然后将
放置在筛选操作之外,可能会使用
链接。每个
都是这样,这就是我的想法。我如何计算满足此参数的单词总数?我是否需要使用.map创建一个新数组,然后计数?或者我可以做一个.split.count吗?@AlfatahKader是的
split.count
(带块)有效。请参阅添加的答案。关于“用红色标记围兜编号”如何?@CarySwoveland作为练习离开;-)这有点高级,但要详细得多。
r = /
    \b            # match a word break
    (?:           # begin a non-capture group
      \p{Alpha}   # match a letter
      |           # or
      (\p{Alpha}) # match a letter in capture group 1
      \p{Alpha}*  # match zero or more letters
      \1          # match the contents of capture group 1
    )             # end the non-capture group
    \b            # match a word break
    /ix           # case-indifferent and free-spacing regex definition modes

str = "How, now is that a brown cow?"

str.gsub(r).count
  #=> 2
str.gsub(r).to_a
  #=> ["that", "a"]

str.scan(r)
  #=> [["t"], [nil]]
sentence_one =  "Label the bib numbers in a red color"

sentence_one.split(' ').keep_if{ |w| w.end_with?(w[0].downcase) & (w.size > 1) }

#=> ["Label", "bib"]
sentence_one.scan(/\S+/).select{|s| s[0].downcase == s[-1].downcase}
# => ["Label", "bib"]