如果哈希中的特定鍵包含或包含某些單詞,我試圖從陣列中洗掉一些哈希。在下面找到陣列:
BANNED_WORDS = ['Hacked', 'hack', 'fraud', 'hacked']
data = [
{
"news_url": "https://www.benzinga.com/markets/cryptocurrency/21/10/23391043/north-vancouver-to-heat-buildings-with-bitcoin-mining",
"image_url": "https://crypto.snapi.dev/images/v1/m/v/fw-69939.jpeg",
"title": "North Vancouver To Heat Buildings With Bitcoin Mining",
"text": "Canadian hack Bitcoin (CRYPTO: BTC) mining firm MintGreen has partnered with state-owned Lonsdale Energy Corporation (LEC) to heat 100 residential and commercial buildings in North Vancouver with recovered energy from crypto mining.",
"source_name": "Benzinga",
"date": "Fri, 15 Oct 2021 12:16:19 -0400",
"topics": [
"mining"
],
"sentiment": "Neutral",
"type": "Article",
"tickers": [
"BTC"
]
},
{
"news_url": "https://u.today/ethereum-20-next-steps-to-mainnet-shared-by-ethereum-foundation",
"image_url": "https://crypto.snapi.dev/images/v1/b/t/10169-69937.jpg",
"title": "Ethereum 2.0 Next Steps to Mainnet Shared by Ethereum Foundation",
"text": "Ethereum (ETH) developers have entered final phase of testing before hotly anticipated ETH1-ETH2 transition",
"source_name": "UToday",
"date": "Fri, 15 Oct 2021 12:11:00 -0400",
"topics": [],
"sentiment": "Neutral",
"type": "Article",
"tickers": [
"ETH"
]
}
]
我正在嘗試洗掉文本或標題包含/包含上面 BANNED_WORDS 陣列中的任何單詞的任何哈希。
我已經嘗試了以下和其他一些變體,但似乎都沒有作業。我是 ruby?? 新手,請有人指出我做錯了什么,謝謝。
data.select{|coin| coin[:text].split(" ").select{ |word| !BANNED_WORDS.include?(word) || coin[:title].split(" ").select{ |word| !BANNED_WORDS.include?(word)}}
所以結果應該是:
filtered_result = [
{
"news_url": "https://u.today/ethereum-20-next-steps-to-mainnet-shared-by-ethereum-foundation",
"image_url": "https://crypto.snapi.dev/images/v1/b/t/10169-69937.jpg",
"title": "Ethereum 2.0 Next Steps to Mainnet Shared by Ethereum Foundation",
"text": "Ethereum (ETH) developers have entered final phase of testing before hotly anticipated ETH1-ETH2 transition",
"source_name": "UToday",
"date": "Fri, 15 Oct 2021 12:11:00 -0400",
"topics": [],
"sentiment": "Neutral",
"type": "Article",
"tickers": [
"ETH"
]
}
]
uj5u.com熱心網友回復:
這是正則運算式的作業。
R = /\b(?:#{BANNED_WORDS.join('|')})\b/
#=> /\b(?:Hacked|hack|fraud|hacked)\b/
data.reject { |h| h[:title].match?(R) || h[:text].match?(R) }
#=> [{:news_url=>"https://u.today/ethereum-20-next-steps...,
# ...
# :tickers=>["ETH"]}]
看到正則運算式#match?.
\b在正則運算式中是一個詞邊界。他們在那里防止匹配,比如說,'haskintosh'和'defraud'。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/321205.html
上一篇:正則運算式在術語之前選擇文本
下一篇:為什么呼叫pppp不回圈?
