【发布时间】:2015-12-28 22:27:48
【问题描述】:
我一直在尝试找出一个正则表达式来只输出三个字母并删除“not”这个词
到目前为止我尝试过的是:
.gsub(/^[A-z]+$/)(依然输出如下)我也在this post about Regex上尝试了所有方法
这是我需要的正则表达式:
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 和答案中引用该变量,而无需定义它。 (续)