Ricardo Neves
Ricardo Neves

Reputation: 515

Remove BR tag from the beginning and end of a string

How can I use something like

return Regex.Replace("/(^)?(<br\s*\/?>\s*)+$/", "", source);

to replace this cases:

<br>thestringIwant => thestringIwant
<br><br>thestringIwant => thestringIwant
<br>thestringIwant<br> => thestringIwant
<br><br>thestringIwant<br><br> => thestringIwant
thestringIwant<br><br> => thestringIwant

It can have multiple br tags at begining or end, but i dont want to remove any br tag in the middle.

Upvotes: 2

Views: 7977

Answers (7)

Adarsh Kumar
Adarsh Kumar

Reputation: 1150

I believe that one should not ignore the power of Regex. If you name the regular expression appropriately then it would not be difficult to maintain it in future.

I have written a sample program which does your task using Regex. It also ignores the character cases and white space at beginning and end. You can try other source string samples you have.

Most important, It would be faster.

using System;
using System.Text.RegularExpressions;

namespace ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            string result;
            var source = @"<br><br>thestringIwant<br><br> => thestringIwant<br/> same <br/> <br/>  ";
            result = RemoveStartEndBrTag(source);
            Console.WriteLine(result);
            Console.ReadKey();
        }

        private static string RemoveStartEndBrTag(string source)
        {
            const string replaceStartEndBrTag = @"(^(<br>[\s]*)+|([\s]*<br[\s]*/>)+[\s]*$)";
            return Regex.Replace(source, replaceStartEndBrTag, "", RegexOptions.IgnoreCase);
        }
    }
}

Upvotes: 0

Ehsan
Ehsan

Reputation: 32681

You can write an extension method to this stuff

 public static string TrimStart(this string value, string stringToTrim)
    {
        if (value.StartsWith(stringToTrim, StringComparison.CurrentCultureIgnoreCase))
        {
            return value.Substring(stringToTrim.Length);
        }
        return value;
    }


    public static string TrimEnd(this string value, string stringToTrim)
    {
        if (value.EndsWith(stringToTrim, StringComparison.CurrentCultureIgnoreCase))
        {                
            return value.Substring(0, value.Length - stringToTrim.Length);
        }
        return value;
    }

you can call it like

 string example = "<br> some <br> test <br>";
 example = example.TrimStart("<br>").TrimEnd("<br>"); //output  some <br> test 

Upvotes: 0

smerlung
smerlung

Reputation: 1519

if you also want it to work with

<br />

then you could use

return Regex.Replace("((:?<br\s*/?>)*<br\s*/?>$|^<br\s*/?>(:?<br\s*/?>)*)", "", source);

EDIT:

Now it should also take care of multiple

<br\s*/?>

in the start and end of the lines

Upvotes: 0

Darren Crabb
Darren Crabb

Reputation: 580

How about doing it in two goes so ...

result1 = Regex.Replace("/^(<br\s*\/?>\s*)+/", "", source);

then feed the result of that into

result2 = Regex.Replace("/(<br\s*\/?>\s*)+$/", "", result1);

It's a bit of added overhead I know but simplifies things enormously, and saves trying to counter match everything in the middle that isn't a BR.

Note the subtle difference between those two .. one matching them at start and one matching them at end. Doing it this way keeps the flexibility of keeping a regular expression that allows for the general formatting of BR tags rather than it being too strict.

Upvotes: 0

Microsoft DN
Microsoft DN

Reputation: 10020

There is no need to use regular expression for it

you can simply use

yourString.Replace("<br>", "");

This will remove all occurances of <br> from your string.

EDIT:

To keep the
tag present in between the string, just use as follows-

var regex = new Regex(Regex.Escape("<br>"));
var newText = regex.Replace("<br>thestring<br>Iwant<br>", "<br>", 1);

newText = newText.Substring(0, newText.LastIndexOf("<br>"));

Response.Write(newText);

This will remove only 1st and last occurance of <br> from your string.

Upvotes: 0

Krishna Sarma
Krishna Sarma

Reputation: 1860

When I see your regular expression, it sounds like there could be spaces allowed with in br tag. So you can try something like:

string s = Regex.Replace(input,@"\<\s*br\s*\/?\s*\>","");

Upvotes: 1

Alex
Alex

Reputation: 23300

A couple of loops would solve the issue and be easier to read and understand (use a regex = tomorrow you look at your own code wondering what the heck is going on)

while(source.StartsWith("<br>")) 
    source = source.SubString(4);
while(source.EndsWith("<br>"))  
    source = source.SubString(0,source.Length - 4);

return source;

Upvotes: 5

Related Questions