2016-08-24 197 views
2

我需要清除短語"not"和主題標籤(#)中的字符串。 (我也有擺脫空間和大寫鎖,並在陣列回他們,但我得到了後三者的照顧。)從字符串中刪除字符串模式和符號

後市展望:

"not12345"  #=> ["12345"] 
" notabc " #=> ["abc"] 
"notone, nottwo" #=> ["one", "two"] 
"notCAPSLOCK" #=> ["capslock"] 
"##doublehash" #=> ["doublehash"] 
"h#a#s#h"  #=> ["hash"] 
"#notswaggerest" #=> ["swaggerest"] 

這是我

代碼
def some_method(string) 
    string.split(", ").map{|n| n.sub(/(not)/,"").downcase.strip} 
end 

以上所有的測試都做了我需要做的事情,除了散列之外。我不知道如何擺脫哈希;我曾嘗試修改正則表達式部分:n.sub(/(#not)/),n.sub(/#(not)/)n.sub(/[#]*(not)/)無濟於事。我如何讓正則表達式刪除#

+0

Woops!在那裏打字。感謝您的注意!你是對的。它應該是[「哈希」]。我會編輯它。謝謝@sln! – Iggy

回答

3
arr = ["not12345", " notabc", "notone, nottwo", "notCAPSLOCK", 
     "##doublehash:", "h#a#s#h", "#notswaggerest"]. 

arr.flat_map { |str| str.downcase.split(',').map { |s| s.gsub(/#|not|\s+/,"") } } 
    #=> ["12345", "abc", "one", "two", "capslock", "doublehash:", "hash", "swaggerest"] 

當塊變量str設置爲"notone, nottwo"

s = str.downcase 
    #=> "notone, nottwo" 
a = s.split(',') 
    #=> ["notone", " nottwo"] 
b = a.map { |s| s.gsub(/#|not|\s+/,"") } 
    #=> ["one", "two"] 

因爲我以前Enumerable#flat_map"one""two"被添加到被返回的數組。當str #=> "notCAPSLOCK"

s = str.downcase 
    #=> "notcapslock" 
a = s.split(',') 
    #=> ["notcapslock"] 
b = a.map { |s| s.gsub(/#|not|\s+/,"") } 
    #=> ["capslock"] 
+0

似乎「一,二」應該是[「一」,「二」]基於期望。 – engineersmnky

+0

謝謝,@engineersmnky,我錯過了。 –

1

你可以使用這個正則表達式來解決你的問題。我測試過了,它適用於所有的測試用例。

/^\s*#*(not)*/ 
  • ^意味着字符串匹配的開始處開始
  • \s*匹配任何空間
  • #*比賽0以上#
  • (not)*短語 「不是」 零次或多次匹配。

注:這個表達式不會對這裏的「不」之前的「#」來的情況下工作,如not#hash將返回#hash

+0

是的,謝謝! – davidhu2000

1

Ruby的正則表達式allow comments,所以匹配井號(# ),您可以逃避它:

"#foo".sub(/\#/, "") #=> "foo" 
+1

你不需要在正常的正則表達式中轉義'#'。你需要這樣做的時候是當你使用'\ x'選項時,它忽略了所有的空格,並允許使用帶#的註釋 – davidhu2000

+0

答案是不正確的。正如@ davidhu2000所說,只有當使用'x'標誌時。 #「#foo」.sub(/#/,「」)#=>「foo」'不會轉義。 –

+0

答案是_NOT_不正確。英鎊符號可以被轉義爲表示擴展的// x或不是//正則表達式中的文字。 – sln

1

有趣的問題,因爲它可以在Ruby中使用最常用的字符串函數:

result = values.map do |string| 
string.strip  # Remove spaces in front and back. 
    .tr('#','')  # Transform single characters. In this case remove # 
    .gsub('not','') # Substitute patterns 
    .split(', ') # Split into arrays. 
end 

p result #=>[["12345"], ["abc"], ["one", "two"], ["CAPSLOCK"], ["doublehash"], ["hash"], ["swaggerest"]] 

我更喜歡這種方式,而不是一個正則表達式,因爲它是很容易理解每​​一行的邏輯。

2

下面是一個使用捕捉不同的技術,你想要什麼,而不是丟棄你不想要的東西多了一個解決方案:(大部分)

a = ["not12345", " notabc", "notone, nottwo", 
"notCAPSLOCK", "##doublehash:","h#a#s#h", "#notswaggerest"] 
a.map do |s| 
    s.downcase.delete("#").scan(/(?<=not)\w+|^[^not]\w+/) 
end 
#=> [["12345"], ["abc"], ["one", "two"], ["capslock"], ["doublehash"], ["hash"], ["swaggerest"]] 

不得不刪除,因爲h#a#s#h#否則刪除本來可以避免像/(?<=not|^#[^not])\w+/