Ruby多行正则表达式

时间:2012-08-10 02:28:56

标签: ruby regex

我有一个ruby多行字符串(称为efixes),如下所示:

ID  STATE LABEL      INSTALL TIME      UPDATED BY ABSTRACT
=== ===== ========== ================= ========== ======================================
1    S    hayo32.02   xxxxxxx xxxxxxxx            xxxxxxxxxxxxxxx
2    S    23434.23    xxxxxxx xxxxxxxx            xxxxxxxxxxxxxxx

STATE codes:
S = STABLE
M = MOUNTED
U = UNMOUNTED
Q = REBOOT REQUIRED
B = BROKEN
I = INSTALLING
R = REMOVING
T = TESTED
P = PATCHED
N = NOT PATCHED
SP = STABLE + PATCHED
SN = STABLE + NOT PATCHED
QP = BOOT IMAGE MODIFIED + PATCHED
QN = BOOT IMAGE MODIFIED + NOT PATCHED
RQ = REMOVING + REBOOT REQUIRED

想要显示以数字开头的行。我遇到了麻烦,似乎没有匹配。我在这里找到了这个解决方案,(我现在还不能真正理解):

efixes_array = efixes.split("\n").select{|x| /\A[0-9]/.match(x)}
io.puts efixes_array.collect{|x| x.scan(/\A[0-9]/)}.flatten

它只匹配数字。我想显示整行。最终结果,我想显示“LABELS”列下的内容。

1 个答案:

答案 0 :(得分:0)

示例代码中的这一行

efixes.split("\n").select{|x| /\A[0-9]/.match(x)}

返回一个数组,其中所有行都以数字开头。