正则表达式匹配三个字母并删除三个字母

时间:2015-12-28 22:27:48

标签: ruby regex

我一直试图找出一个正则表达式,只输出三个字母,并删除单词" not"

到目前为止,我所尝试的是:

这是我需要的正则表达方式:

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

有没有办法可以做到这一点?请记住,我有多个其他三个字母组合,所有字母都是字母。

4 个答案:

答案 0 :(得分:3)

为何选择正则表达式?你的生活太复杂了:

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

简短,易读,易读,享受Ruby的力量。

答案 1 :(得分: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

答案 2 :(得分: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"] 

如果这不是一次性计算,请考虑将来文本格式是否可能发生变化。如果可能,请实施一种您认为在更改后最不可能需要修改的方法。

答案 3 :(得分:-1)

这应该做:

&#34; bash:line。?:(。?):&#34;

这将从bash到&#34;:&#34;在一行之后,在&#34;之前的三个或更多字母中返回&#34;:#34;

你可以在这里测试一下 http://rubular.com/