简体   繁体   English

使用连字符作为正则表达式的字符串

[英]Use hyphen as string for regex

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. 其中代理名称和flatfilelogsource名称将检索字符串中的各个值。

but am getting an error says [xy] range in reverse order.. which should be because of the agent name as it contains hyphen. 但是收到错误消息说[xy]范围是反向顺序。这应该是由于代理名称,因为它包含连字符。

so I am basically looking for a method which could escape hyphen as a string like @ does to \\ 所以我基本上是在寻找可能逃避连字符作为一个字符串的方法等等@确实给\\

For the error message that you recibe, I think "[@"+agentName+"]" should be writen as "\\[@"+agentName+"\\]" like in most regular expression flavors, although I don't know the implementation of C#. 对于您收到的错误消息,我认为"[@"+agentName+"]"应该写为"\\[@"+agentName+"\\]"就像大多数正则表达式一样,尽管我不知道C#。

Quick explanation 快速说明

The square brackets forms a class, a kind of collection of valid characters for to match. 方括号构成一个类,是要匹配的有效字符的一种集合。 Those classes can use ranges as [0-9] for matching numbers in decimal notation or [0-7] to match them in octal notation. 这些类可以使用范围[0-9]来匹配十进制表示法中的数字,或者使用[0-7]来匹配八进制表示法中的数字。 And to match series of characters like [ae] which match a, b, c, d or e. 并匹配[ae]与a,b,c,d或e匹配的字符。 But ranges can't go in reverse order. 但是范围不能相反。 So [az] is a valid range but [za] is not. 因此[az]是有效范围,但[za]不是有效范围。 To avoid creating a character class, you must escape the square brackets with a backslask 为避免创建字符类,必须用反斜杠转义方括号

\\ is the regex escape character. \\是正则表达式转义符。

This should work: 这应该工作:

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

Edit: Although seeing this sentence confuses me as to what you are actually asking for: 编辑:虽然看到这句话使我对您实际要求的内容感到困惑:

where agent name and flatfilelogsource name will retrieve me the respective values in the string. 其中代理名称和flatfilelogsource名称将检索字符串中的各个值。

If you're trying to pull data out of the regex match, this should help you: http://www.regular-expressions.info/named.html 如果您尝试从正则表达式匹配项中提取数据,这应该对您有所帮助: http : //www.regular-expressions.info/named.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正则表达式匹配字符串中的连字符0或1次 - Regex to match a hyphen in a string 0 or 1 times 正则表达式从字符串的末尾删除数字和连字符(-) - Regex to remove digits & hyphen(-) from end of a string 使用正则表达式匹配连字符后的所有内容 - Use regex to match everything after hyphen 正则表达式用字符串中的一个连字符替换多个连字符? asp.net C# - RegEx to replace more than one hyphen with one hyphen in a string? asp.net C# 正则表达式为1到150之间的有效字符串,以逗号和连字符分隔 - Regex for valid string between 1 and 150 separated by comma and hyphen 正则表达式允许单个连字符和下划线,但不能在字符串的开头或结尾 - Regex to allow single hyphen and underscore but not at beginning or end of string 正则表达式检测连字符 - Regex to detect hyphen character 正则表达式为英文字符,连字符和下划线 - Regex for english characters, hyphen and underscore 正则表达式从字符串中精确匹配 11 位电话号码并从 c# 中的匹配中删除连字符(-) - regex to exact match 11 digit phone number from string and remove hyphen(-) from match in c# 使用Regex遍历字符串并搜索3个连续的连字符,并将其替换为[space] [hyphen] [space] - using Regex to iterate over a string and search for 3 consecutive hyphens and replace it with [space][hyphen][space]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM