gpt4 book ai didi

Ruby grep 没有过滤掉不匹配的输入

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

鉴于我有这门课:

class A
attr_accessor :a, :b, :c, :d
end

鉴于这些是实例方法:

A.instance_methods(false)
=> [:a, :a=, :b, :b=, :c, :c=, :d, :d=]

我尝试用“=”过滤掉数组中的任何项目:

A.instance_methods(false).grep(/[^=]/)
=> [:a, :a=, :b, :b=, :c, :c=, :d, :d=]

A.instance_methods(false).grep(/[a-zA-Z]/)
=> [:a, :a=, :b, :b=, :c, :c=, :d, :d=]

但它不会过滤掉带“=”的项目。我做错了什么?

最佳答案

I try to filter out any item in array with an "=".

在这种类型的过滤中,您可能需要使用 grep_v相反,它是 grep 的逆函数。它会删除与正则表达式匹配的所有内容。

现在你的表达变得简单了:

[:a, :a=, :b, :b=, :c, :c=, :d, :d=].grep_v(/=/)
=> [:a, :b, :c, :d]

关于Ruby grep 没有过滤掉不匹配的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51813896/

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