c# Regular Expression

Hi,
I'm trying to replace some broken links in a database and need help with the following:

Broken URL:
<a title="" href="http://mywebsite.com/399">Music In June 2005</a>

I want to change it to:
<a title="" href="http://mywebsite.com/Search.php?ContentID=399">Music In June 2005</a>

I need to do this in c#.  

Thanks
imran0092Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Terry WoodsIT GuruCommented:
Match with regular expression:
href="http://mywebsite[.]com/(\d+)"
And replace with:
href="http://mywebsite.com/Search.php?ContentID=$1"

You'll need to escape the double quotes, and ensure you use a global replace option.
0
Dan CraciunIT ConsultantCommented:
Try this, in C#:
resultString = Regex.Replace(subjectString, @"http://mywebsite\.com/(.*?)>", "http://mywebsite.com/Search.php?ContentID=$1>");

Open in new window


HTH,
Dan
0
Terry WoodsIT GuruCommented:
Slight to Dan's solution, in case it matches more than it should with the .* pattern in the match:
resultString = Regex.Replace(subjectString, @"http://mywebsite\.com/(\d+)>", "http://mywebsite.com/Search.php?ContentID=$1>");

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Dan CraciunIT ConsultantCommented:
If you use digits (I wanted to allow the possibility for a alphanumeric ID), then you'll have to match the " too:
resultString = Regex.Replace(subjectString, @"http://mywebsite\.com/(\d+"")>", "http://mywebsite.com/Search.php?ContentID=$1>");

Open in new window

0
käµfm³d 👽Commented:
...and ensure you use a global replace option.
Unnecessary, as all replaces in C# are global (well, unless you use a particular version of the Replace method, but we're not here!)    = )
0
imran0092Author Commented:
Hi,
I will test these solutions asap.
Thanks
0
Dan CraciunIT ConsultantCommented:
So...
You accepted a non-working solution, and gave it a B rating. Why?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.