0
votes

I'm trying to use regex replace to change this:

/category/45/p-e--sportswear/47/shorts-skorts

into:

/45/47/

I can strip all but the numbers but i'm not sure how to keep the forward slashes while removing other non alphanumeric characters.

urlReferrer = Regex.Replace(urlReferrer, @"[^\d]", string.Empty);

Can someone help me with the correct pattern please?

2
why you won't want to get //45//47/ as output? Use [^\/\d]+ regex and replace all the matched characters with empty string. But it gives //45//47/Avinash Raj

2 Answers

2
votes

You can use:

urlReferrer = Regex.Replace(urlReferrer, @"/[^/\d]+", string.Empty);
//=> /45/47

RegEx Demo

0
votes

You could do this instead of a regular expression:

StringBuilder builder = new StringBuilder();
bool first = true;
bool lastNumeric = true;
bool atLeastOneNumeric = false;
foreach (var part in urlReferrer.Split('/'))
{
    if (part.Length > 0 && part.All(char.IsDigit))
    {
        if (!first)
            builder.Append("/");
        builder.Append(part);
        lastNumeric = true;
        atLeastOneNumeric = true;
    }
    else
    {
        lastNumeric = false;
    }

    first = false;
}

if (!lastNumeric && atLeastOneNumeric)
    builder.Append("/");

urlReferrer = builder.ToString();

This is braking the string by the '/' and then checking which tokens are numeric and appends them to the result. Additionally it keeps track to see if the first token was numeric then there would not be a '/' before it, or if the last token is numeric then there would not be a '/' after it. It also makes sure to not include any '/'s if there are no numeric values.

Here's a shorter version using string.Join and Linq

var tokens = urlReferrer.Split('/');

urlReferrer = string.Join("/", tokens.Where(s => s.Length > 0 && s.All(char.IsDigit)));

if(urlReferrer.Length > 0)
{
    if (tokens.First().Length == 0 || !tokens.First().All(char.IsDigit))
        urlReferrer = "/" + urlReferrer;

    if (tokens.Last().Length == 0 || !tokens.Last().All(char.IsDigit))
        urlReferrer += "/";
}

This simply splits the string by / then joins the numeric values together with / as the separator. Then it checks to see if there were any numbers before checking if the first an last values where numeric or not to determine if a preceding and/or a following / is needed.