The Be Sure Blog

Code Snippets | Problem Solving | Tips & Tricks

The Be Sure Blog banner

Use Regex to target text in between HTML tags

posted on 16.1.2023 by Below Surface in "C#"

In this example, we have a C#/.NET application that is rendering out a .pdf file, generated with Telerik Document Processing. We receive multiple text blocks as strings, including HTML tags like <br />, <b> and </b>. To target the text in between <b></b> tags, we use Regex.

using System.Text.RegularExpressions;

In the method, which is called with the string as an argument, for example "<b>I will be bold</b>", we add this code:

string regex = @"<b>\s*(.+?)\s*</b>";
var match = Regex.Match(item, regex);

if (match.Success == true) { thisBlock.InsertText(FontFamilyBold, match.Groups[1].Value); } else { thisBlock.InsertText(FontFamily, item); }

The output will be "I will be bold". If the input is "I won't be bold", the output will still be in the regular font style.

Tags:

c#
regex