搜索特定数字(以英寸为单位),其中该数字不是较大表达式的一部分
Search for a specific number (in inch) where the number isn't part of a larger expression
我想使用 Excel 公式,当单元格中的文本包含术语“2”(两英寸)时,returns 单元格中的正确索引。这是可能的search
函数。
要注意的是,我只想查找实际为“2”的实例,而不是您有其他表达式(例如“1/2”或“12”)的情况。请参见下图举例说明 search
在哪些地方有效,在哪些地方无效。
使用:
=SEARCH(" 2"""," " & A1)-1
这个公式有三个个小技巧:
- 我们搜索 {space} 2 "
- 我们在字符串的开头放置一个空格
- 我们通过从
位置减一来计算空白
编辑#1:
这可能会更好。使用 A3 中的数据和 B2 中的字符串尝试:
=IFERROR(IF(LEFT(A3,2)=$B,1,SEARCH(" " & $B, A3)+1),0)
如果我没看错,要求是:
1) 如果它以 2" 开头(后跟 space)
2) 字符串中间有 2"(每边有一个 space)
3) 字符串以 2" 结尾(前面有一个 space)它应该是 "OK",否则为零
如果满足这些要求,此公式应该有效:
=IF(OR(LEFT(A2,3)="2"" ",ISNUMBER(SEARCH(" 2"" ",A2)),RIGHT(A2,3)=" 2"""),"OK",0)
-- 或者您可能必须将其用于第二行,具体取决于您的要求 --
=IF(OR(LEFT(A2,2)="2""",ISNUMBER(SEARCH(" 2"" ",A2)),RIGHT(A2,3)=" 2"""),"OK",0)
我认为使用正则表达式的 VBA 解决方案将是最简单的,以便能够 return 像 1 1/2" 这样的测量。
要输入此用户定义函数 (UDF),alt-F11
打开 Visual Basic 编辑器。
确保您的项目在 Project Explorer window 中突出显示。
然后,从顶部菜单 select Insert/Module
和
将下面的代码粘贴到打开的 window 中。
要使用此用户定义函数 (UDF),请输入类似
的公式
=FindMeasure(A1,$E)
在某些单元格中,E1 包含类似 2" 或 1 1/2 的值"
Option Explicit
Function FindMeasure(sSearch As String, ByVal sMeasure As String)
Dim RE As Object, MC As Object, SM As Variant
Dim sPat As String
sPat = "\D(\s+)" & sMeasure & "|^" & sMeasure
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = False
.MultiLine = True
.Pattern = sPat
End With
If RE.test(sSearch) = True Then
Set MC = RE.Execute(sSearch)
SM = MC(0).submatches(0)
FindMeasure = MC(0).firstindex + Len(SM) + IIf(Len(SM) > 0, 2, 1)
Else
FindMeasure = 0
End If
End Function
编辑: 查看我的回答表明在某些情况下,不正确的结果将被 returned。
- 如果测量前有一个以数字结尾的 "word",例程将无法识别该测量。这可以通过确保在测量值之前的字符串中至少有一个非数字(通过修改正则表达式)来避免。但是,如果整个单词由数字组成,则无法识别测量值。
- 如果该行以
SPACE
开头,则无法识别测量值。这可以通过修改代码和正则表达式来解决这种可能性。
- 如果包含测量值的单元格或包含字符串的单元格为空白,则结果将不正确。这可以通过修改代码来测试这些条件来避免。
修改代码
Option Explicit
Function FindMeasure(sSearch As String, ByVal sMeasure As String)
Dim RE As Object, MC As Object, SM As Variant
Dim sPat As String
sPat = "(\S*\D\S*\s+)" & sMeasure & "|(^\s*)" & sMeasure
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = False
.MultiLine = True
.Pattern = sPat
End With
If RE.test(sSearch) = True And _
Len(sSearch) > 0 And _
Len(sMeasure) > 0 Then
Set MC = RE.Execute(sSearch)
SM = MC(0).submatches(0) & MC(0).submatches(1)
FindMeasure = MC(0).firstindex + Len(SM) + 1
Else
FindMeasure = 0
End If
End Function
sMeasure = 2 的正则表达式解释
(\S*\D\S*\s+)2"|(^\s*)2"
(\S*\D\S*\s+)2"|(^\s*)2"
选项:不区分大小写; ^$ 匹配换行符
- Match this alternative
(\S*\D\S*\s+)2"
- Match the regex below and capture its match into backreference number 1
(\S*\D\S*\s+)
- Match the character string “2"” literally
2"
- Or match this alternative
(^\s*)2"
创建于RegexBuddy
我想使用 Excel 公式,当单元格中的文本包含术语“2”(两英寸)时,returns 单元格中的正确索引。这是可能的search
函数。
要注意的是,我只想查找实际为“2”的实例,而不是您有其他表达式(例如“1/2”或“12”)的情况。请参见下图举例说明 search
在哪些地方有效,在哪些地方无效。
使用:
=SEARCH(" 2"""," " & A1)-1
这个公式有三个个小技巧:
- 我们搜索 {space} 2 "
- 我们在字符串的开头放置一个空格
- 我们通过从 位置减一来计算空白
编辑#1:
这可能会更好。使用 A3 中的数据和 B2 中的字符串尝试:
=IFERROR(IF(LEFT(A3,2)=$B,1,SEARCH(" " & $B, A3)+1),0)
如果我没看错,要求是:
1) 如果它以 2" 开头(后跟 space)
2) 字符串中间有 2"(每边有一个 space)
3) 字符串以 2" 结尾(前面有一个 space)它应该是 "OK",否则为零
如果满足这些要求,此公式应该有效:
=IF(OR(LEFT(A2,3)="2"" ",ISNUMBER(SEARCH(" 2"" ",A2)),RIGHT(A2,3)=" 2"""),"OK",0)
-- 或者您可能必须将其用于第二行,具体取决于您的要求 --
=IF(OR(LEFT(A2,2)="2""",ISNUMBER(SEARCH(" 2"" ",A2)),RIGHT(A2,3)=" 2"""),"OK",0)
我认为使用正则表达式的 VBA 解决方案将是最简单的,以便能够 return 像 1 1/2" 这样的测量。
要输入此用户定义函数 (UDF),alt-F11
打开 Visual Basic 编辑器。
确保您的项目在 Project Explorer window 中突出显示。
然后,从顶部菜单 select Insert/Module
和
将下面的代码粘贴到打开的 window 中。
要使用此用户定义函数 (UDF),请输入类似
的公式=FindMeasure(A1,$E)
在某些单元格中,E1 包含类似 2" 或 1 1/2 的值"
Option Explicit
Function FindMeasure(sSearch As String, ByVal sMeasure As String)
Dim RE As Object, MC As Object, SM As Variant
Dim sPat As String
sPat = "\D(\s+)" & sMeasure & "|^" & sMeasure
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = False
.MultiLine = True
.Pattern = sPat
End With
If RE.test(sSearch) = True Then
Set MC = RE.Execute(sSearch)
SM = MC(0).submatches(0)
FindMeasure = MC(0).firstindex + Len(SM) + IIf(Len(SM) > 0, 2, 1)
Else
FindMeasure = 0
End If
End Function
编辑: 查看我的回答表明在某些情况下,不正确的结果将被 returned。
- 如果测量前有一个以数字结尾的 "word",例程将无法识别该测量。这可以通过确保在测量值之前的字符串中至少有一个非数字(通过修改正则表达式)来避免。但是,如果整个单词由数字组成,则无法识别测量值。
- 如果该行以
SPACE
开头,则无法识别测量值。这可以通过修改代码和正则表达式来解决这种可能性。 - 如果包含测量值的单元格或包含字符串的单元格为空白,则结果将不正确。这可以通过修改代码来测试这些条件来避免。
修改代码
Option Explicit
Function FindMeasure(sSearch As String, ByVal sMeasure As String)
Dim RE As Object, MC As Object, SM As Variant
Dim sPat As String
sPat = "(\S*\D\S*\s+)" & sMeasure & "|(^\s*)" & sMeasure
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = False
.MultiLine = True
.Pattern = sPat
End With
If RE.test(sSearch) = True And _
Len(sSearch) > 0 And _
Len(sMeasure) > 0 Then
Set MC = RE.Execute(sSearch)
SM = MC(0).submatches(0) & MC(0).submatches(1)
FindMeasure = MC(0).firstindex + Len(SM) + 1
Else
FindMeasure = 0
End If
End Function
sMeasure = 2 的正则表达式解释
(\S*\D\S*\s+)2"|(^\s*)2"
(\S*\D\S*\s+)2"|(^\s*)2"
选项:不区分大小写; ^$ 匹配换行符
- Match this alternative
(\S*\D\S*\s+)2"
- Match the regex below and capture its match into backreference number 1
(\S*\D\S*\s+)
- Match the character string “2"” literally
2"
- Match the regex below and capture its match into backreference number 1
- Or match this alternative
(^\s*)2"
创建于RegexBuddy