2015-12-28 98 views
1

我一直在試圖正則表達式找出輸出只有三個字母,並刪除單詞「不」正則表達式匹配三個字母和刪除三個字母

我試過到目前爲止是:

一切

這是我需要regexed:

bash: line 1: drs: command not found 
bash: line 2: tep: command not found 
bash: line 3: ldo: command not found 
bash: line 4: tep: command not found 
bash: line 5: txw: command not found 
bash: line 6: tep: command not found 
bash: line 7: jfp: command not found 
bash: line 8: mys: command not found 
bash: line 9: jhf: command not found 
bash: line 10: mjw: command not found 
bash: line 11: czw: command not found 
bash: line 12: txh: command not found 
bash: line 13: krn: command not found 
bash: line 14: sct: command not found 
bash: line 15: jad: command not found 

我希望它只是輸出:

drs 
tep 
ldo 
tep 
txw 
tep 
jfp 
mys 
jhf 
mjw 
czw 
txh 
krn 
sct 
jad 

有沒有一種方法,我可以做到這一點?請記住我有多個其他三個字母組合,並帶有字母表中的所有字母。

+0

總是在那個地方的信件?我的意思是,總是「bash:line xxxx:ABC:....」? – zon7

+0

@ zon7是的,他們總是在同一個地方,我會編輯帖子掛在.. – Bam

+0

請閱讀「[問]」和「[mcve]」。有沒有工作代碼?是否有樣本輸入和您的預期輸出? –

回答

3

爲什麼是正則表達式?你是過於複雜的生活:

def three_letters_excluding_not(text) 
    text 
     .split(/\W+/) 
     .select{|w| w.length == 3} 
     .reject{|w| w=="not} 
end 

短,容易,可讀性強,欣賞紅寶石的力量。

+0

什麼是'w' for ..? – Bam

+1

@Bam更多信息在http://stackoverflow.com/questions/7622369/ruby-extracting-words-from-string'\ W'表示_non-word_ – Caridorc

+1

' w'是一個塊變量 –

2

這似乎並不像一個良好的使用正則表達式的,因爲你要處理的領域:

str = "bash: line 14: krn: command not found" 
str.split(': ')[2] # => "krn" 

這裏有更詳盡的測試:

[ 
    'bash: line 1: drs: command not found', 
    'bash: line 2: tep: command not found', 
    'bash: line 3: ldo: command not found', 
    'bash: line 4: tep: command not found', 
    'bash: line 5: txw: command not found', 
    'bash: line 6: tep: command not found', 
    'bash: line 7: jfp: command not found', 
    'bash: line 8: mys: command not found', 
    'bash: line 9: jhf: command not found', 
    'bash: line 10: mjw: command not found', 
    'bash: line 11: czw: command not found', 
    'bash: line 12: txh: command not found', 
    'bash: line 13: krn: command not found', 
    'bash: line 14: sct: command not found', 
    'bash: line 15: jad: command not found', 
].each do |str| 
    puts str.split(': ')[2] 
end 
# >> drs 
# >> tep 
# >> ldo 
# >> tep 
# >> txw 
# >> tep 
# >> jfp 
# >> mys 
# >> jhf 
# >> mjw 
# >> czw 
# >> txh 
# >> krn 
# >> sct 
# >> jad 

如果你不知道如何很多空格將圍繞:分隔符,使用strip從所捕獲的單詞中刪除前導空格和尾部空格:

str.split(':')[2].strip 
+0

所以'2'空格? – Bam

+0

不是真的,你可以計算出多少個。 –

+0

哦,我的意思是'[2]'搜索粗略的兩個空格? – Bam

-1

這應該這樣做:

「bash:line。 :():」

這將讓一切從慶典,直到‘:’行後,返回一組中的三個或更多的字母前的‘:’

你可以測試一下這裏 http://rubular.com/

+0

我不想返回'三個或更多字母'只有三個, – Bam

+1

它應該只返回三個,但它準備返回更多。它只是工作:) – zon7

1
str =<<_ 
bash: line 1: drs: command not found 
bash: line 2: tep: command not found 
bash: line 3: not: command not found 
bash: line 4: tep: command not found 
bash: line 5: txw: command not found 
_ 

r =/
    \d:\s+ # match a digit, colon and one or more spaces 
    \K  # forget everything matched so far 
    .{3} # match any three characters 
    /x  # extended/free-spacing regex definition mode 

str.scan r 
    #=> ["drs", "tep", "not", "tep", "txw"] 

如果你不想 「不」:

str.scan(r) - ["not"] 
    #=> ["drs", "tep", "tep", "txw"] 

如果這不是一關C考慮文本格式是否會在未來發生變化。如果可能,實施一種您認爲最不可能在更改後需要修改的方法。

相關問題