有没有办法从右到左匹配正则表达式?我正在寻找的是正则表达式
MODULE WAS INSERTED EVENT
LOST SIGNAL ON E1/T1 LINK OFF
CRC ERROR EVENT
CLK IS DIFF FROM MASTER CLK SRC OF
来自此输入
CLI MUX trap received: (022) CL-B MCL-2ETH MODULE WAS INSERTED EVENT 07-05-2010 12:08:40
CLI MUX trap received: (090) IO-2 ML-1E1 EX1 LOST SIGNAL ON E1/T1 LINK OFF 04-06-2010 09:58:58
CLI MUX trap received: (094) IO-2 ML-1E1 EX1 CRC ERROR EVENT 04-06-2010 09:58:59
CLI MUX trap received: (009) CLK IS DIFF FROM MASTER CLK SRC OFF 07-05-2010 12:07:32
如果我可以从右到左完成匹配,我可以在(EVENT | OFF)右边写一些东西,直到第二次出现多个空格[] +
我今天所做的最好的事情是使用正则表达式获取从(022)到EVENT的所有内容
CLI MUX trap received: \([0-9]+\)[ ]+(.*[ ]+(EVENT|OFF))
但那不是我想要的那样:)
编辑:它的用语是什么?它实际上是我们拥有的过滤器的配置字符串,但我猜它是使用标准的GNU C Regex库。
edit2:我喜欢关于缩短长度的答案,但Amarghosh可能更像是我想要的。我真的不知道为什么我不考虑只是缩短长度:
^.{56}(.{39}).*$
非常感谢快速回答...
答案 0 :(得分:19)
在.NET中,您可以使用RightToLeft
option:
Regex RE = new Regex(Pattern, RegexOptions.RightToLeft);
Match theMatch = RE.Match(Source);
答案 1 :(得分:3)
使用正则表达式,您可以简单地替换它:
^.{56}|.{19}$
使用空字符串。
但实际上,您只需要使用子字符串函数将字符串从“位置56”剪切为“字符串长度 - 19”。这比正则表达式更容易,很多。
以下是JavaScript中的一个示例,其他语言的工作方式大致相同:
var lines = [
'CLI MUX trap received: (022) CL-B MCL-2ETH MODULE WAS INSERTED EVENT 07-05-2010 12:08:40',
'CLI MUX trap received: (090) IO-2 ML-1E1 EX1 LOST SIGNAL ON E1/T1 LINK OFF 04-06-2010 09:58:58',
'CLI MUX trap received: (094) IO-2 ML-1E1 EX1 CRC ERROR EVENT 04-06-2010 09:58:59',
'CLI MUX trap received: (009) CLK IS DIFF FROM MASTER CLK SRC OFF 07-05-2010 12:07:32'
];
for (var i=0; i<lines.length; i++) {
alert( lines[i].substring(56, lines[i].length-19) );
}
答案 2 :(得分:2)
如果保证令牌被多个空格分隔,并且EVENT|OFF
之前的字符串中的单词只保证被一个空格分隔 - 那么你可以查找单个空格分隔的单词,然后是空格后跟EVENT
或OFF
var s = "CLI MUX trap received: (022) CL-B MCL-2ETH MODULE WAS INSERTED EVENT 07-05-2010 12:08:40"
+ "\nCLI MUX trap received: (090) IO-2 ML-1E1 EX1 LOST SIGNAL ON E1/T1 LINK OFF 04-06-2010 09:58:58"
+ "\nCLI MUX trap received: (094) IO-2 ML-1E1 EX1 CRC ERROR EVENT 04-06-2010 09:58:59"
+ "\nCLI MUX trap received: (009) CLK IS DIFF FROM MASTER CLK SRC OFF 07-05-2010 12:07:32"
var r = /\([0-9]+\).+?((?:[^ ]+ )* +(?:EVENT|OFF))/g;
var m;
while((m = r.exec(s)) != null)
console.log(m[1]);
输出:
MODULE WAS INSERTED EVENT
LOST SIGNAL ON E1/T1 LINK OFF
CRC ERROR EVENT
CLK IS DIFF FROM MASTER CLK SRC OFF
正则表达式:/\([0-9]+\).+?((?:[^ ]+ )* +(?:EVENT|OFF))/g
\([0-9]+\) #digits in parentheses followed by
.+? #some characters - minimum required (non-greedy)
( #start capturing
(?:[^ ]+ )* #non-space characters separated by a space
` +` #more spaces (separating string and event/off -
#backticks added for emphasis), followed by
(?:EVENT|OFF) #EVENT or OFF
) #stop capturing
答案 3 :(得分:1)
输入文件是否适合这样的固定宽度表格文本?因为如果确实如此,那么最简单的解决方案就是从第56列到第94列采取每行的正确substring
。
在Unix中,您可以使用cut
命令:
cut -c56-94 yourfile
在Java中,你可以这样写:
String[] lines = {
"CLI MUX trap received: (022) CL-B MCL-2ETH MODULE WAS INSERTED EVENT 07-05-2010 12:08:40",
"CLI MUX trap received: (090) IO-2 ML-1E1 EX1 LOST SIGNAL ON E1/T1 LINK OFF 04-06-2010 09:58:58",
"CLI MUX trap received: (094) IO-2 ML-1E1 EX1 CRC ERROR EVENT 04-06-2010 09:58:59",
"CLI MUX trap received: (009) CLK IS DIFF FROM MASTER CLK SRC OFF 07-05-2010 12:07:32",
};
for (String line : lines) {
System.out.println(line.substring(56, 94));
}
打印:
MODULE WAS INSERTED EVENT
LOST SIGNAL ON E1/T1 LINK OFF
CRC ERROR EVENT
CLK IS DIFF FROM MASTER CLK SRC OFF
这很可能没有必要,但是这样的工作(as seen on ideone.com):
line.replaceAll(".* \\b(.+ .+) \\S+ \\S+", "$1")
正如您所看到的,它不是非常易读,您必须知道您的正则表达式才能真正了解正在发生的事情。
基本上你将它与每一行相匹配:
.* \b(.+ .+) \S+ \S+
然后用匹配的1组替换它。这依赖于两个连续空格的使用,专门用于分隔此表中的列。
答案 4 :(得分:0)
怎么样
.{56}(.*(EVENT|OFF))
答案 5 :(得分:0)
您可以进行面向字段的处理,而不是正则表达式吗?在awk / sh中,这看起来像:
< $datafile awk '{ print $(NF-3), $(NF-2) }' | column
这似乎比指定正则表达式更清晰。