c# – How can I further optimise this regular expression? – Education Career Blog

I have just coded the below regular expression. I have a mini rich text editor on a web page (very similar to the one I am using to post this question) and I want to make use of a double asterisk to indicate which words/phrases should be wrapped in a strong tag. The aim is to allow the user to add pre-defined HTML elements without actually having to submit HTML.

Here is the Unit test for it:

<TestMethod()> Public Sub Regular_Expression_Replaces_Double_Asterix_With_Strong_Tags()

    'Arrange
    Dim originalString As String = "This the start of the text. **This should be wrapped with a strong tag**. But this part should **not**. ** Note: this part should be left alone since it isn't closed off."
    Dim htmlFormattedString As String = "This the start of the text. <strong>This should be wrapped with a strong tag</strong>. But this part should <strong>not</strong>. ** Note: this part should be left alone since it isn't closed off."

    'Act
    originalString = ItemTemplate.FormatTemplateToHtml(originalString)

    'Assert
    Assert.AreEqual(htmlFormattedString, originalString)

End Sub

And here is the working code:

Public Shared Function FormatTemplateToHtml(ByVal value As String) As String

    Dim formattedValue As String = value
    Dim pattern As String = "(\*{2})(.*?)(\*{2})"
    Dim regExMatches As MatchCollection = Regex.Matches(value, pattern)

    For Each regExMatch As Match In regExMatches

        'This is the part I feel could be improved?
        Dim replaceableTag As String = regExMatch.Groups(0).Value
        Dim reformattedTag As String = String.Format("<strong>{0}</strong>", regExMatch.Groups(2).Value)
        formattedValue = formattedValue.Replace(replaceableTag, reformattedTag)

    Next

    Return formattedValue

End Function

Maybe I am over-optimising this, but I want to know if this can be made more efficient?

Note: I use both VB.Net and C# professionally so even though this example is in VB.Net (as the project this is for, is using VB.Net) C# answers are welcomed.

,

Why don’t you just use the Replace method?

Dim outputText As String =
    Regex.Replace(inputText, "\*{2}(.*?)\*{2}", "<strong>$1</strong>")

Leave a Comment