Regex - Match an end html tag if start tag is not present

i want to get an ending html tag like </EM> only if somewhere before it i.e. before any previous tags or text there is no starting <EM> tag my sample string is

ddd d<STRONG>dfdsdsd dsdsddd<EM>ss</EM>r and</EM>and strong</STRONG>

in this string the output should be </EM> and this also the second </EM> because it lacks the starting <EM>. i have tried

(?!=<EM>.*)</EM>

but it doesnt seem to work please help thnks

Answers


I am not sure regex is best suited for this kind of task, since tags can always be nested.

Anyhow, a C# regex like:

(?<!<EM>[^<]+)</EM>

would only bring the second </EM> tag

Note that:

  • ?! is a negative look*ahead* which explains why both </EM> are found. So... (?!=<EM>.*)xxx actually means capture xxx if it is not followed by =<EM>.*. I am not sure you wanted to include an = in there
  • ?<! is a negative look*behind*, more suited to what you wanted to do, but which would not work with java regex engine, since this look-behind regex does not have an obvious maximum length.

However, with a .Net regex engine, as tested on RETester, it does work.


Need Your Help

Bootstrap different img size at lg, md sm and xs

html css responsive-design twitter-bootstrap-3

I need to make a image change it's size if the viewer has a smartphone or a PC. This achieves what I want but it's a bad way to do it because of loading the img 4 times and it's redundant:

Ninject + Auto Discovery

c# ninject inversion-of-control

Here's the deal: I want to create a C# console app. When run, this app will look in a particular folder for dll's with classes that implement a particular interface, and then run a method on those

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.