0

I have the following HTML within a multi-line text field.

href="/Pages/33/33998.aspx">(Answer 33998)

I need to remove the folder structure from the URL. In my Regex I can find the match but I cant seem to get the replacement to work. Anyone have any suggestions?

Heres the start of my regex:

<a href=\"\/Pages\/\d{1,}\/\d{3,}[.]aspx"></a>

So I have this:

Regex.Replace(Convert.ToString(properties.AfterProperties["Description"]), @"<a href='/Pages\/\d{1,}\/\d{3,}[.]aspx'>", "TEST", RegexOptions.IgnoreCase);

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • [Don't use regex](http://stackoverflow.com/a/1732454/1895201) – DGibbs Apr 16 '14 at 13:14
  • possible duplicate of [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – John Saunders Apr 16 '14 at 13:31

2 Answers2

1

You could use Regex.Replace method and use groups to identify what you do need to preserve from your match, like below:

var sample = @"
    href='/Pages/33/33998.aspx'>(Answer 33998)
    href='/Pages/12345.aspx'>(no match)
    href='/Pages/lala/12345.aspx'>(no match)
    ";
Console.WriteLine(Regex.Replace(sample, 
//    |-- First group--|    |-- Second group --|
    @"(href=['""]/Pages)/\d+(/\d{3,}\.aspx['""])", "$1$2"));
// ------------- First and second groups -------------^
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
0

You could, instead of using a regex, use a simple split by / and take the last array item.

string[] split = yourUri.Split("/");
Michael
  • 621
  • 5
  • 17