简体   繁体   English

如何进一步优化此正则表达式?

[英]How can I further optimise this regular expression?

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. 目的是允许用户添加预定义的HTML元素,而无需实际提交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 . 注意:我专业地使用VB.Net和C#,因此即使该示例在VB.Net中(作为该项目的目标,正在使用VB.Net), 也欢迎使用C#答案

Why don't you just use the Replace method? 您为什么不只使用Replace方法?

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

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

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