搜索具有固定结尾的子串
Searching for a substring with a fixed end
我想在其他字符串的集合(在 Excel 列中列出)中搜索以某个短语结尾的子字符串。
所以假设我想在其他字符串中搜索字符串 "BLUE MOON" 并且我想确定子字符串在 "BLUE MOON" 的 "N" 之后结束以避免在例如情况下结果为 TRUE "BLUE MOONLIGHT"。换句话说,我需要的是搜索单词的任何部分,但仅限左手部分。右手应该有一个固定的边框,即零附加字符。
另一方面,如果开头不同,我需要积极的结果,例如 "DARK BLUE MOON" 应该导致 TRUE。因此完全平等不是一种选择。
我想使用 Find
,但我认为这是不可能的。 Find
似乎不接受除 *
之外的任何通配符。
这里有一些词供您测试:
预期的搜索结果是肯定的:
BLUE MOON
DARK BLUE MOON
LIGHT BLUE MOON
预期搜索结果为负::
BLUE MOONLIGHT
LAST BLUE MOONSHINE
BLUE MOONDANCE
也欢迎任何提示。现在我正在使用以下函数删除单词(工作正常,除了它还会删除前面提到的预期搜索结果为负的案例):
Sub testingXXX()
Dim ws As Worksheet
Set ws = ActiveWorkbook.ActiveSheet
Dim aCell As Range, bCell As Range, aSave As String, y As Long
MyAr = Split("*BLUE MOON", ",")
For y = LBound(MyAr) To UBound(MyAr)
With ws
Set aCell = .Columns(1).Find(what:=MyAr(y), LookIn:=xlValues, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aSave = aCell.Address
Do
If bCell Is Nothing Then
Set bCell = .Range("A" & aCell.row)
Else
Set bCell = Union(bCell, .Range("A" & aCell.row))
End If
Set aCell = .Columns(1).FindNext(after:=aCell)
Loop Until aCell.Address = aSave
End If
Set aCell = Nothing
End With
Next y
If Not bCell Is Nothing Then bCell.EntireRow.Delete
End Sub
这就是发明正则表达式的原因。如果您使用 \b
指定字边框,则只会匹配完全匹配的词组 "BLUE MOON"
,不允许添加其他字符。以下将匹配包含 "BLUE MOON"
的字符串:
Const PHRASES As String = "BLUE MOON,DARK BLUE MOON,LIGHT BLUE MOON,BLUE MOONLIGHT,LAST BLUE MOONSHINE,BLUE MOONDANCE"
Dim re
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "\bBLUE MOON\b"
Dim w
For Each w In Split(PHRASES, ",")
If re.Test(w) Then
Debug.Print w & " = Match"
Else
Debug.Print w & " = No match"
End If
Next
输出(为便于阅读而对齐):
BLUE MOON = Match
DARK BLUE MOON = Match
LIGHT BLUE MOON = Match
BLUE MOONLIGHT = No match
LAST BLUE MOONSHINE = No match
BLUE MOONDANCE = No match
如果要确保 在 "MOON"
之后的字符串 完全没有任何附加内容,请包括字符串结尾锚点 ($
)在你的模式中:
re.Pattern = "\bBLUE MOON$"
我想在其他字符串的集合(在 Excel 列中列出)中搜索以某个短语结尾的子字符串。
所以假设我想在其他字符串中搜索字符串 "BLUE MOON" 并且我想确定子字符串在 "BLUE MOON" 的 "N" 之后结束以避免在例如情况下结果为 TRUE "BLUE MOONLIGHT"。换句话说,我需要的是搜索单词的任何部分,但仅限左手部分。右手应该有一个固定的边框,即零附加字符。 另一方面,如果开头不同,我需要积极的结果,例如 "DARK BLUE MOON" 应该导致 TRUE。因此完全平等不是一种选择。
我想使用 Find
,但我认为这是不可能的。 Find
似乎不接受除 *
之外的任何通配符。
这里有一些词供您测试:
预期的搜索结果是肯定的:
BLUE MOON DARK BLUE MOON LIGHT BLUE MOON
预期搜索结果为负::
BLUE MOONLIGHT LAST BLUE MOONSHINE BLUE MOONDANCE
也欢迎任何提示。现在我正在使用以下函数删除单词(工作正常,除了它还会删除前面提到的预期搜索结果为负的案例):
Sub testingXXX()
Dim ws As Worksheet
Set ws = ActiveWorkbook.ActiveSheet
Dim aCell As Range, bCell As Range, aSave As String, y As Long
MyAr = Split("*BLUE MOON", ",")
For y = LBound(MyAr) To UBound(MyAr)
With ws
Set aCell = .Columns(1).Find(what:=MyAr(y), LookIn:=xlValues, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aSave = aCell.Address
Do
If bCell Is Nothing Then
Set bCell = .Range("A" & aCell.row)
Else
Set bCell = Union(bCell, .Range("A" & aCell.row))
End If
Set aCell = .Columns(1).FindNext(after:=aCell)
Loop Until aCell.Address = aSave
End If
Set aCell = Nothing
End With
Next y
If Not bCell Is Nothing Then bCell.EntireRow.Delete
End Sub
这就是发明正则表达式的原因。如果您使用 \b
指定字边框,则只会匹配完全匹配的词组 "BLUE MOON"
,不允许添加其他字符。以下将匹配包含 "BLUE MOON"
的字符串:
Const PHRASES As String = "BLUE MOON,DARK BLUE MOON,LIGHT BLUE MOON,BLUE MOONLIGHT,LAST BLUE MOONSHINE,BLUE MOONDANCE"
Dim re
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "\bBLUE MOON\b"
Dim w
For Each w In Split(PHRASES, ",")
If re.Test(w) Then
Debug.Print w & " = Match"
Else
Debug.Print w & " = No match"
End If
Next
输出(为便于阅读而对齐):
BLUE MOON = Match
DARK BLUE MOON = Match
LIGHT BLUE MOON = Match
BLUE MOONLIGHT = No match
LAST BLUE MOONSHINE = No match
BLUE MOONDANCE = No match
如果要确保 在 "MOON"
之后的字符串 完全没有任何附加内容,请包括字符串结尾锚点 ($
)在你的模式中:
re.Pattern = "\bBLUE MOON$"