正则表达式 - 量词 {x,y} 没有任何内容

Regex - Quantifier {x,y} following nothing

我正在创建一个基本的文本编辑器,我正在使用正则表达式来实现查找和替换功能。为此,我得到了这段代码:

Private Function GetRegExpression() As Regex
    Dim result As Regex
    Dim regExString As [String]
    ' Get what the user entered
    If TabControl1.SelectedIndex = 0 Then
        regExString = txtbx_Find2.Text
    ElseIf TabControl1.SelectedIndex = 1 Then
        regExString = txtbx_Find.Text
    End If

    If chkMatchCase.Checked Then
        result = New Regex(regExString)
    Else
        result = New Regex(regExString, RegexOptions.IgnoreCase)
    End If

    Return result
End Function

这是查找方法

 Private Sub FindText()
    ''
    Dim WpfTest1 As New Spellpad.Tb
    Dim ElementHost1 As System.Windows.Forms.Integration.ElementHost = frm_Menu.Controls("ElementHost1")
    Dim TheTextBox As System.Windows.Controls.TextBox = CType(ElementHost1.Child, Tb).ctrl_TextBox
    ''
    ' Is this the first time find is called?
    ' Then make instances of RegEx and Match
    If isFirstFind Then
        regex = GetRegExpression()
        match = regex.Match(TheTextBox.Text)
        isFirstFind = False
    Else
        ' match.NextMatch() is also ok, except in Replace
        ' In replace as text is changing, it is necessary to
        ' find again
        'match = match.NextMatch();
        match = regex.Match(TheTextBox.Text, match.Index + 1)

    End If

    ' found a match?
    If match.Success Then
        ' then select it
        Dim row As Integer = TheTextBox.GetLineIndexFromCharacterIndex(TheTextBox.CaretIndex)
        MoveCaretToLine(TheTextBox, row + 1)
        TheTextBox.SelectionStart = match.Index
        TheTextBox.SelectionLength = match.Length

    Else
        If TabControl1.SelectedIndex = 0 Then
            MessageBox.Show([String].Format("Cannot find ""{0}""   ", txtbx_Find2.Text), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        ElseIf TabControl1.SelectedIndex = 1 Then
            MessageBox.Show([String].Format("Cannot find ""{0}""   ", txtbx_Find.Text), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
        isFirstFind = True
    End If
End Sub

当我运行程序时出现错误:

好像我不能使用这些但我确实需要。我该如何解决这个问题?

?* 量词 正则表达式 :

  • ? 用于指定某些东西是可选的,例如 b?au 可以匹配 bauau
  • * 表示它所绑定的组可以重复零次、一次或多次:例如 ba*u 可以沐浴 bubaubaau, baaaaaaaau,...

现在大多数正则表达式使用{l,u}作为第三种模式,l是重复次数的下限,u 出现次数的上限。所以 ?{0,1} 取代, *{0,} 取代。

现在,如果您提供的 前面没有任何 字符,显然,正则表达式解析器不知道您的意思。换句话说,如果你这样做(使用csharp,但思路普遍适用):

$ csharp
Mono C# Shell, type "help;" for help

Enter statements below.
csharp> Regex r = new Regex("fo*bar");
csharp> r.Replace("Fooobar fooobar fbar fobar","<MATCH>");    
"Fooobar <MATCH> <MATCH> <MATCH>"
csharp> r.Replace("fooobar far qux fooobar quux fbar echo fobar","<MATCH>");
"<MATCH> far qux <MATCH> quux <MATCH> echo <MATCH>"

如果你想做一个“原始文本查找和替换”,你应该使用string.Replace

编辑:

另一种处理它们的方法是转义特殊的正则表达式字符。具有讽刺意味的是,您可以通过用正则表达式替换它们来做到这一点 ;)。

Private Function GetRegExpression() As Regex
    Dim result As Regex
    Dim regExString As [String]
    ' Get what the user entered
    If TabControl1.SelectedIndex = 0 Then
        regExString = txtbx_Find2.Text
    ElseIf TabControl1.SelectedIndex = 1 Then
        regExString = txtbx_Find.Text
    End If

    'Added code
    Dim baseRegex As Regex = new Regex("[\.$^{\[(|)*+?]")
    regExString = baseRegex.Replace(regExString,"$0")
    'End added code

    If chkMatchCase.Checked Then
        result = New Regex(regExString)
    Else
        result = New Regex(regExString, RegexOptions.IgnoreCase)
    End If

    Return result
End Function