【问题标题】:ruby regexp to find a word that does not contain digitsruby regexp 查找不包含数字的单词
【发布时间】:2016-10-24 04:48:28
【问题描述】:

我希望我的正则表达式返回一个枚举器,该枚举器将返回包含非数字单词的块,我能得到的最好方法是什么?

我尝试了以下方法:

regexp= /(?=\w+)(?=^(?:(?!\d+).)*$)/
"this is a number 1234".split(regexp) # ["this is a number 1234"]

我期望 (?=\w+) 应该确保它是否是单词并且我期望 (?=^(?:(?!\d+).)*$) 确保它不包含任何数字。

我期待一个输出:

["this", "is", "a", "number"]

【问题讨论】:

    标签: ruby regex gsub


    【解决方案1】:

    scansplit 更容易:

    regexp = /\b[[:alpha:]]+\b/
    p "this is a number 1234".scan(regexp)
    # => ["this", "is", "a", "number"]
    

    【讨论】:

      【解决方案2】:

      尝试关注。

      p "this is a number 1234".scan(/\D+/).first.split(' ')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-10
        • 2015-02-17
        • 2018-06-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多