我正在创建一个基本的文本编辑器,我正在使用正则表达式来实现查找和替换功能。为此,我得到了以下代码:
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.
好像我不能使用这些,但我真的需要。如何解决这个问题?
?
和*
是正则表达式中的量词:
-
?
用于指定某些内容是可选的,例如b?au
可以同时匹配bau
和au
。 -
*
意味着与它结合的基团可以重复零次、一次或多次:例如ba*u
可以洗澡bu
、bau
、baau
、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