简体   繁体   English

C#正则表达式题

[英]C# regular expression question

Can anybody help me to form a regular expression to search the following string:谁能帮我形成一个正则表达式来搜索以下字符串:

<b>The</b> <b>brown</b> <b>fox</b> jumped over the <b>lazy</b> <b>dog</b>.

The expression should match <b>The</b> <b>brown</b> <b>fox</b> as one match then proceed to match <b>lazy</b> <b>dog</b> .该表达式应匹配<b>The</b> <b>brown</b> <b>fox</b>作为一个匹配项,然后继续匹配<b>lazy</b> <b>dog</b> In this example, the expression should return two matches only, thanks.在这个例子中,表达式应该只返回两个匹配项,谢谢。

Is this what you're looking for?这是你要找的吗?

Regex r = new Regex(@"<b>[^<]*</b>(?:\s*<b>[^<]*</b>)*");

String input = @"<b>The</b> <b>brown</b> <b>fox</b> jumped over the <b>lazy</b> <b>dog</b>.";
foreach (Match m in r.Matches(input))
{
  Console.WriteLine(m.Value);
}

output: output:

<b>The</b> <b>brown</b> <b>fox</b>
<b>lazy</b> <b>dog</b>

This would work with your specific example:这将适用于您的具体示例:

@"The brown fox|lazy dog"

Furthermore, if you need to match any more simple phrases, just append |the simple phrase to this pattern.此外,如果您需要匹配任何更简单的短语,只需 append |the simple phrase即可。

The brown fox|lazy dog

The above is the regex that would generate two matches from your given input.以上是将从给定输入生成两个匹配项的正则表达式。

RegEx really isn't suited to parsing HTML . RegEx 真的不适合解析 HTML A much better solution would be to use the Html Agility Pack更好的解决方案是使用Html 敏捷包

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

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