• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

Replace String with Regular Expression

I have a string like this:

"&nbsp;&nbsp;&nbsp;<img src=\"images/SmallDot.gif\">&nbsp;<a class=\"ArticleItem\" href=\"Index.aspx?ArticleID=70239&ChannelID=16\" title=\"zzznzbxznb\">Some Text Here</a>Outer Text 1<a href=\"Link1\">Text in Link1</a>Outer Text 2<a href=\"Link 2\">Text in Link 2</a> (Date/XXX nxnmzxc) <a></a>Something here"

I want to write a regular expression to replace above string to this:

"&nbsp;&nbsp;&nbsp;<img src=\"images/SmallDot.gif\">&nbsp;<a class=\"ArticleItem\" href=\"Index.aspx?ArticleID=70239&ChannelID=16\" title=\"zzznzbxznb\">Some Text Here</a>Outer Text 1<a href=\"Link1\">Text in Link1</a>Outer Text 2 <a></a>Something here"

As you can see, string in Date format and first link before it must be replaced.
I wrote an expression but it replaced all link before date string
This is my expression

"([^\v]*?)<a[^\v]*?href=\"[^\v]*?\">[^<]+</a>([^=])+\\([^\v]*?/[^\v]*?\\)([^\v]*?)"

Regex.Replace(_strInput, _strOuterDatePattern, "$1$3", RegexOptions.IgnoreCase);


Please help me !
0
software22
Asked:
software22
  • 3
1 Solution
 
Thandava VallepalliCommented:
try this


output = Regex.Replace( input, "([^\v]*?)<a[^\v<]*?href=\"[^\v<]*?\">[^<]+</a>([^=<])+?\\([^\v]*?/[^\v]*?\\)([^\v]*?)", "$1$3", RegexOptions.IgnoreCase );            


itsvtk
0
 
Thandava VallepalliCommented:
Here is the test code.....

====================
using System;
using System.Text.RegularExpressions;

class Demo
{
      public static void Main()
      {
            string input = "&nbsp;&nbsp;&nbsp;<img src=\"images/SmallDot.gif\">&nbsp;" +
                  "<a class=\"ArticleItem\" href=\"Index.aspx?ArticleID=70239&ChannelID=16\" title=\"zzznzbxznb\">Some Text Here</a>" +
                  "Outer Text 1<a href=\"Link1\">Text in Link1</a>" +
                  "Outer Text 2<a href=\"Link 2\">Text in Link 2</a> (Date/XXX nxnmzxc) <a></a>Something here";
            string output = "";
            
            output = Regex.Replace( input, "([^\v]*?)<a[^\v<]*?href=\"[^\v<]*?\">[^<]+</a>([^=<])+?\\([^\v]*?/[^\v]*?\\)([^\v]*?)", "$1$3", RegexOptions.IgnoreCase );
            Console.WriteLine( "Input: {0}  \n\nOutput: {1}", input, output );
      }
}
=================================

Output.....
===================================================
Input: &nbsp;&nbsp;&nbsp;<img src="images/SmallDot.gif">&nbsp;<a class="ArticleItem" href="Index.aspx?ArticleID=70239&ChannelID=16" title="zzznzbxznb">Some Text Here</a>Outer Text 1<a href="Link1">Text in Link1</a>Outer Text 2<a href="Link 2">Text in Link 2</a> (Date/XXX nxnmzxc) <a></a>Something here  

Output: &nbsp;&nbsp;&nbsp;<img src="images/SmallDot.gif">&nbsp;<a class="ArticleItem" href="Index.aspx?ArticleID=70239&ChannelID=16" title="zzznzbxznb">Some Text Here</a>Outer Text 1<a href="Link1">Text in Link1</a>Outer Text 2 <a></a>Something here
====================================================

itsvtk
0
 
software22Author Commented:
Thanks itsvtk. But I don't understand what +? mean.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now