0

I have developed a regex in which checks a specific pattern in a log file..

string looks something like this :

05-20-2013 15:57:09.334715 [del1-dhp-26330] Read 100 entries from syslog file test

and my regex function is

\d\d-\d\d-\d\d\d\d \d\d:\d\d:\d\d.\d\d\d\d\d\d [@"+agentName+"]"+" Read 100 entries from "+flatFileLogSourceName;

where agent name and flatfilelogsource name will retrieve me the respective values in the string.

but am getting an error says [x-y] range in reverse order.. which should be because of the agent name as it contains hyphen.

so I am basically looking for a method which could escape hyphen as a string like @ does to \

4

2 回答 2

2

对于你recibe的错误消息,我认为"[@"+agentName+"]"应该"\[@"+agentName+"\]"像大多数正则表达式一样写,虽然我不知道C#的实现。

快速解释

方括号形成一个类,一种用于匹配的有效字符的集合。这些类可以使用范围[0-9]来匹配十进制表示法中的数字或[0-7]八进制表示法中匹配它们。并匹配一系列字符[a-e],例如匹配 a、b、c、d 或 e。但是范围不能以相反的顺序排列。[a-z]有效范围也是如此,但[z-a]不是。为避免创建字符类,您必须使用反斜杠转义方括号

于 2013-05-20T14:27:06.990 回答
1

\ 是正则表达式转义字符。

这应该有效:

"\d{2}\-\d{2}\-\d{4} \d{2}:\d{2}:\d{2}\.\d{6} \["+agentName+"\] Read 100 entries from "+flatFileLogSourceName

编辑:虽然看到这句话让我对你实际要求的东西感到困惑:

其中代理名称和平面文件日志源名称将为我检索字符串中的相应值。

如果您尝试从正则表达式匹配中提取数据,这应该对您有所帮助:http ://www.regular-expressions.info/named.html

于 2013-05-20T14:05:32.387 回答