正则表达式 - 量词 {x,y} 不跟任何东西 [英] Regex - Quantifier {x,y} following nothing
问题描述
我正在创建一个基本的文本编辑器,我正在使用正则表达式来实现查找和替换功能.为此,我得到了以下代码:
I'm creating a basic text editor and I'm using regex to achieve a find and replace function. To do this I've gotten this code:
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
这就是Find方法
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
当我运行程序时出现错误:
When I run the program I get errors:
- 对于
?
,解析?"- 量词 {x,y} 后面没有.
;和 - 对于
*
,解析*" - 量词{x,y}后面什么都没有.
好像我不能使用这些,但我真的需要.我该如何解决这个问题?
It's as if I can't use these but I really need to. How can I solve this problem?
推荐答案
?
和 *
是正则表达式中的量词:
?
and *
are quantifiers in regular expressions:
?
用于指定某些东西是可选的,例如b?au
可以匹配bau
和au代码>.
*
表示它所绑定的组可以重复零次、一次或多次:例如ba*u
可以洗澡bu
,bau
,baau
,baaaaaaaau
,...
?
is used to specify that something is optional, for instanceb?au
can match bothbau
andau
.*
means the group with which it binds can be repeated zero, one or multiple times: for instanceba*u
can bathbu
,bau
,baau
,baaaaaaaau
,...
现在大多数正则表达式使用{l,u}
作为第三种模式,l
是某事物次数的下限重复,u
是出现次数的上限.所以 ?
被 {0,1}
替换,*
被 {0,}
替换.
Now most regular expressions use {l,u}
as a third pattern with l
the lower bound on the number of times something is repeated, and u
the upper bound on the number of occurences. So ?
is replaced by {0,1}
and *
by {0,}
.
现在,如果您在它们之前提供没有任何字符,显然正则表达式解析器不知道您的意思.换句话说,如果你这样做(使用了csharp
,但这些想法普遍适用):
Now if you provide them without any character before them, evidently, the regex parser doesn't know what you mean. In other words if you do (used csharp
, but the ideas are generally applicable):
$ 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
.
If you wish to do a "raw text find and replace", you should use string.Replace
.
编辑:
另一种处理它们的方法是转义特殊的正则表达式字符.具有讽刺意味的是,您可以通过用正则表达式替换它们来做到这一点;)
Another way to process them is by escaping special regex characters. Ironically enough, you can do this by replacing them by a regex ;).
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
这篇关于正则表达式 - 量词 {x,y} 不跟任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!