【问题标题】:Regex to match three letters and delete three letters正则表达式匹配三个字母并删除三个字母
【发布时间】:2015-12-28 22:27:48
【问题描述】:

我一直在尝试找出一个正则表达式来只输出三个字母并删除“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

有什么办法可以做到这一点吗?请记住,我还有多个其他三个字母组合,包含所有字母表。

【问题讨论】:

  • 字母总是在那个地方吗?我的意思是,总是“bash: line xxxx: ABC: ....”?
  • @zon7 是的,他们总是在同一个地方,我会编辑帖子等等……
  • 请阅读“How to Ask”和“minimal reproducible example”。有工作代码吗?是否有示例输入和您的预期输出?
  • 警告:不要在正则表达式中使用[A-z] 它可以匹配您所期望的大小写 ASCII 字母,但它也匹配代码点位于 @ 之间的几个标点符号987654328@ 和A。请改用[A-Za-z],或使用不区分大小写的标志(例如/[a-z]/i)。
  • 您可以通过多种方式改进您的问题: 1. 说明您想要做什么,而不参考您认为应该采用的方法(例如,使用正则表达式)。 2. 举例时,确保每个输入值都是有效的 Ruby 对象。这里不清楚您的文本是字符串还是字符串数组。你应该写"bash: line 1:...."["bash: line 1:...]。 3. 为作为示例输入的每个对象分配一个变量(例如,str = "bash: line 1:...."),以便读者可以在 cmets 和答案中引用该变量,而无需定义它。 (续)

标签: ruby regex


【解决方案1】:

为什么是正则表达式?你的生活过于复杂了:

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

简短、简单、易读,享受 Ruby 的强大功能。

【讨论】:

【解决方案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] 搜索两个空格?
  • @Bam,split 返回一个数组,[2] 是数组的索引。
【解决方案3】:
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"] 

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

【讨论】:

    【解决方案4】:

    应该这样做:

    "bash: line.?: (.?):"

    这将获取从 bash 到行之后的“:”的所有内容,并在一组中返回“:”之前的三个或更多字母

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

    【讨论】:

    • 我不想回three or more letters只有三个,
    • 它应该只返回三个,但它准备返回更多。它只是工作:)
    猜你喜欢
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    相关资源
    最近更新 更多