简体   繁体   English

使用c#regex排除字符串匹配

[英]Exclude string match using c# regex

I am new to regular expressions.I am trying to find the Images doesn't having BORDER. 我是正则表达式的新手。我试图找到图像没有BORDER。 So the result should second Image.The text which is trying to match using regex is below. 所以结果应该是第二张Image。试图使用正则表达式进行匹配的文本如下。

<IMG onerror="this.errored=true;" USEMAP="#Map-43" BORDER="0"/>
<IMG onerror="this.errored=true;" USEMAP="#Map-43" />
<IMG onerror="this.errored=true;" USEMAP="#Map-43" BORDER="0"/>    

I tried the following regex but didn't worked 我尝试了以下正则表达式,但没有奏效

<IMG\\s[^((>)&(?!BORDER)]*>

So can any one help on this please? 那么有人可以帮忙吗?

You can use HtmlAgilityPack to parse html 您可以使用HtmlAgilityPack来解析html

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

var imgs = doc.DocumentNode.Descendants("img")
    .Where(n => n.Attributes["border"] == null)
    .ToList();

PS: See also this: RegEx match open tags except XHTML self-contained tags PS:另请参见: RegEx匹配除XHTML自包含标记之外的开放标记

The better choice would be to use an html parser for such a problem. 更好的选择是使用html解析器来解决这个问题。

But your main regex problem here is that you put your lookahead into a character class, that way all character where treated as literal characters. 但是你的主要正则表达式问题是你把你的预测放到一个字符类中,这样所有字符都被视为文字字符。

<IMG\s(?:(?!BORDER)[^>])*>

should work better. 应该工作得更好。 See it on Regexr . 在Regexr上看到它

But thats only to explain your regex problem. 但这仅仅是为了解释你的正则表达式问题。 To solve your programming task please use LB answer . 要解决您的编程任务,请使用LB答案

Working example: 工作范例:

String html = "<IMG onerror=\"this.errored=true;\" USEMAP=\"#Map-43\" BORDER=\"0\"/><IMG onerror=\"this.errored=true;\" USEMAP=\"#Map-43\" /><IMG onerror=\"this.errored=true;\" USEMAP=\"#Map-43\" BORDER=\"0\"/>";
Console.WriteLine(Regex.Matches(html, @"<IMG\s(?:(?!BORDER)[^>])*>").Cast<Match>().ToList()[0]);
Console.ReadLine();

Another way is to get the "no border attribute" images client-side with the jQuery and CSS selectors: 另一种方法是使用jQuery和CSS选择器获取客户端的“无边界属性”图像:

$img = $('img').not('[border]');

Links: 链接:

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

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