简体   繁体   English

从字符串的开头和结尾删除BR标签

[英]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. 它可以在开头或结尾有多个br标签,但我不想在中间删除任何br标签。

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;

When I see your regular expression, it sounds like there could be spaces allowed with in br tag. 当我看到您的正则表达式时,听起来好像br标签中可以包含空格。 So you can try something like: 因此,您可以尝试以下操作:

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

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. 这将从字符串中删除所有出现的<br>

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. 这只会从字符串中删除<br>第一个和最后一次出现。

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 

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 在行的开头和结尾

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. 我知道这会增加一些开销,但会大大简化事情,并且省去了尝试在非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. 这样做可以保持正则表达式的灵活性,该正则表达式允许对BR标签进行常规格式化,而不必过于严格。

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. 我编写了一个示例程序,使用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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM