?
Solved

Replace String with Regular Expression

Posted on 2005-03-15
4
Medium Priority
?
233 Views
Last Modified: 2010-04-16
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
Comment
Question by:software22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 14

Expert Comment

by:Thandava Vallepalli
ID: 13552874
try this


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


itsvtk
0
 
LVL 14

Accepted Solution

by:
Thandava Vallepalli earned 2000 total points
ID: 13552889
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
 

Author Comment

by:software22
ID: 13571863
Thanks itsvtk. But I don't understand what +? mean.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question