gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:31:31 25 4
gpt4 key购买 nike

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

我有办法做到这一点吗?请记住,我还有多个其他三个字母组合,所有字母都是字母表中的字母。

最佳答案

为什么要使用正则表达式?你让你的生活过于复杂了:

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

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

关于ruby - 正则表达式匹配三个字母并删除三个字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34501707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com