正则表达式 - 量词{x,y}无效

时间:2015-01-07 03:21:19

标签: regex vb.net search

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

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

当我运行程序时,我收到错误:

  • ?parsing "?" - Quantifier {x,y} following nothing.;和
  • 适用于*parsing "*" - Quantifier {x,y} following nothing.

好像我无法使用这些,但我真的需要。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

?*正则表达式中的量词

  • ?用于指定某些内容是可选的,例如b?au可以匹配bauau
  • *表示与其绑定的组可以重复零次,一次或多次:例如ba*u可以停放bubau,{{1} },baau,...

现在大多数正则表达式使用baaaaaaaau作为第三种模式,其中{l,u}是重复次数的下限,l是上层受限发生的数量。因此u?{0,1}替换为*

现在,如果你在他们之前提供没有任何字符,显然,正则表达式解析器并不知道你的意思。换句话说,如果你这样做(使用{0,},但这些想法通常适用):

csharp

如果您希望执行" 原始文本查找和替换",则应使用string.Replace

修改

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

$ 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>"