Excel精确字匹配

假设我在A1单元格中有“Vegas is great”。 我想写一个公式,在细胞中寻找确切的单词“gas”。 拉斯维加斯≠天然气,但我发现唯一的search公式:

=ISNUMBER(SEARCH("gas",lower(A1)) 

返回true。 有没有办法做精确匹配? 我最好喜欢它是不区分大小写,我相信通过将A1包装在lower()中是满足的。

我相信要正确覆盖的情况下,你必须在术语“气”和search术语之前和之后填充空格。 这将确保气体将在一个单元的开始或结束时被发现,并且也防止在任何单词的中间被发现。 你的文章没有说明标点符号是否可以存在于文件中,但是为了适应search周围的标点符号填充空间将无法正常工作,你将不得不包含“gas”,“gas!”等等的情况,以允许任何标点符号。 如果您担心捕捉像“gas.cost”或类似的值,则可以在标点符号search周围使用相同的填充。

=Or(ISNUMBER(SEARCH(" gas ", " "&A1&" ")),ISNUMBER(SEARCH(" gas. ", " "&A1&" ")))

是一个基本的search应该返回自己的气体,或“气”。 在“气”之后填充一个空格。 在search中,它会将其作为句子中的最后一个单词或单元格的结尾。

编辑:删除了一个括号。

查找function区分大小写。 searchfunction不是。 如果使用SEARCH,则不需要LOWERfunction 。

SEARCH( <find_text>,<within_text>,[optional] <start_num>

将空格中的find_textwithin_text包裹起来,然后执行search。

搜索功能

B1中的公式是,

=ISNUMBER(SEARCH(" gas ", " "&A1&" "))

根据需要填写。

人们也可以使用VBA中的正则expression式来实现这一点。 在正则expression式中,“\ b”代表一个字边界。 单词边界被定义为单词和非单词字符之间的位置或该行的开始或结束。 单词字符是[A-Za-z0-9_](字母,数字和下划线)。 因此,可以使用这个UDF。 您需要注意的是,包含非单词字符(例如连字符)的单词可能与您预期的不同。 如果你正在处理非英文字母,模式将需要修改。

但代码相当紧凑。

 Option Explicit Function reFindWord(FindWord As String, SearchText As String, Optional MatchCase As Boolean = False) As Boolean Dim RE As Object Dim sPattern As String Set RE = CreateObject("vbscript.regexp") sPattern = "\b" & FindWord & "\b" With RE .Pattern = sPattern .ignorecase = Not MatchCase reFindWord = .test(SearchText) End With End Function 

我认为覆盖search词周围所有可能的标点符号的唯一方法是创build一个自定义macros函数。 使用增强的拆分function将句子标记为单词数组,然后在数组中search匹配项。

增强的拆分functionhttps://msdn.microsoft.com/zh-cn/library/aa155763

如何创build自定义macroshttp://www.wikihow.com/Create-a-User-Defined-Function-in-Microsoft-Excel

创buildFindEngWord函数的代码

 Public Function FindEngWord(ByVal TextToSearch As String, ByVal WordToFind As String) As Boolean Dim WrdArray() As String Dim text_string As String Dim isFound As Boolean isFound = False text_string = TextToSearch WrdArray() = Split(text_string) isFound = False For i = 0 To UBound(WrdArray) If LCase(WrdArray(i)) = LCase(WordToFind) Then isFound = True End If Next i FindEngWord = isFound End Function Public Function Split(ByVal InputText As String, _ Optional ByVal Delimiter As String) As Variant ' This function splits the sentence in InputText into ' words and returns a string array of the words. Each ' element of the array contains one word. ' This constant contains punctuation and characters ' that should be filtered from the input string. Const CHARS = ".!?,;:""'()[]{}" Dim strReplacedText As String Dim intIndex As Integer ' Replace tab characters with space characters. strReplacedText = Trim(Replace(InputText, _ vbTab, " ")) ' Filter all specified characters from the string. For intIndex = 1 To Len(CHARS) strReplacedText = Trim(Replace(strReplacedText, _ Mid(CHARS, intIndex, 1), " ")) Next intIndex ' Loop until all consecutive space characters are ' replaced by a single space character. Do While InStr(strReplacedText, " ") strReplacedText = Replace(strReplacedText, _ " ", " ") Loop ' Split the sentence into an array of words and return ' the array. If a delimiter is specified, use it. 'MsgBox "String:" & strReplacedText If Len(Delimiter) = 0 Then Split = VBA.Split(strReplacedText) Else Split = VBA.Split(strReplacedText, Delimiter) End If End Function 

可以从你的Excel表格中调用。

 =FindEngWord(A1,"gas") 

我认为这将处理你打算处理的所有情况:

 =OR(ISNUMBER(SEARCH(" gas",LOWER(A1), 1 )), LEFT(A1,3)= "gas") 

我在search的“气”之前加了一个空格。 如果气体是单元格中的唯一单词或单元格中的第一个单词,则此函数的右侧部分将处理该情况。