简体   繁体   English

正则表达式匹配句子中的特定单词

[英]regexp match in specific word in a sentence

I'm searching though IIS log files looking for lines where the 6th word contains h3.asp 我正在搜索IIS日志文件,以查找第6个单词包含h3.asp的行

Tried to make a regular expression, but it's failing: 试图做一个正则表达式,但是失败了:

string text = @"2010-08-28 00:12:15 W3SVC591993719 192.168.10.13 GET /forum/h3.asp g=forum 80 - 10.10.10.10 Opera/9.80+(S60;+SymbOS;+Opera+Mobi/499;+U;+no)+Presto/2.4.18+Version/10.00 http://www.somesite.com/forum/default.aspx?g=posts&m=28078& www.somesite.com 200 0 0 62";

string pattern = @"(\w* ){5}\w*h3\.asp";

Console.WriteLine(Regex.IsMatch(text, pattern));

In the above sample I'm expecting a match, but clearly something is wrong. 在上面的示例中,我期待一场比赛,但显然有些问题。

How about this? 这个怎么样?

 string pattern = @"(\S+\s){5}\S*?h3\.asp";

\\S will match all non-whitespace, so this regex will match 5 groups consisting of non-whitespace followed by a single whitespace, followed by anything that contains "h3.asp" \\ S将匹配所有非空白,因此此正则表达式将匹配5个组,其中包括非空白,后跟一个空白,然后是任何包含“ h3.asp”的内容

\\w only includes az \\ w仅包含az

you might be better off doing something along the lines of: 您最好按照以下方式做点事情:

@"(.+?\s+){5}.+?h3\.asp";

I didn't get to test it, but hopefully you'll see the difference 我没有进行测试,但希望您能看到其中的不同

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM