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

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
0
imran0092
Asked:
imran0092
1 Solution
 
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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