The .NET web system I'm working on allows the end user to input HTML formatted text in some situations. In some of those places, we want to leave all the tags, but strip off any trailing break tags (but leave any breaks inside the body of the text.)
What's the best way to do this? (I can think of ways to do this, but I'm sure they're not the best.)
As @Mitch said,
// using System.Text.RegularExpressions;
/// <summary>
/// Regular expression built for C# on: Thu, Sep 25, 2008, 02:01:36 PM
/// Using Expresso Version: 2.1.2150, http://www.ultrapico.com
///
/// A description of the regular expression:
///
/// Match expression but don't capture it. [\<br\s*/?\>], any number of repetitions
/// \<br\s*/?\>
/// <
/// br
/// Whitespace, any number of repetitions
/// /, zero or one repetitions
/// >
/// End of line or string
///
///
/// </summary>
public static Regex regex = new Regex(
@"(?:\<br\s*/?\>)*$",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
regex.Replace(text, string.Empty);
Small change to bdukes code, which should be faster as it doesn't backtrack.
public static Regex regex = new Regex(
@"(?:\<br[^>]*\>)*$",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
regex.Replace(text, string.Empty);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With