我有一些数据流与一些条目。
;
彼此分开。;
我想匹配条目中的所有字段。我使用负前瞻断言从[:print:]
POSIX字符类中减去分号,但似乎不适用于长度受限的字段。
我的数据:
[1427894078] SERV;ICE ALERT: example.com ;Current Load;CRITICAL;SOFT;3;CRITICAL - load average: 1.96, 1.29, 0.59
我的正则表达式(PCRE):
((?!;)[[:print:]]{1,60});((?!;)[[:print:]]{0,60})
我期望获得:
Match 1:
Group 1: [1427894078] SERV
Group 2: ICE ALERT: example.com
Match 2:
Group 1: Current Load
Group 2: CRITICAL
Match 3:
Group 1: SOFT
Group 2: 3
我错了:
Match 1:
Group 1: [1427894078] SERV;ICE ALERT: example.com ;Current Load
Group 2: CRITICAL;SOFT;3;CRITICAL - load average: 1.96, 1.29, 0.59
但是:Kua zxsw指出
你非常接近。你的正则表达式的唯一问题是你没有在量化集群中包含先行:
(?!;)[[:print:]]{1,60}
现在它匹配正确的字符块(参见现场演示(?:(?!;)[[:print:]]){1,60}
):
here
但是,有一个更好的选择(参见现场演示((?:(?!;)[[:print:]]){1,60});((?:(?!;)[[:print:]]){0,60})
):
here