寻找专家,希望有人能够帮助我。 我有一个很长的联想产品清单,我正在寻找一种方法从中提取其产品编号。 问题是该号码可能位于单元格的任何位置。 首先,我们知道数字有 10 个字符长,它总是包含数字和字母,并且它们之间没有特殊字符。 有时数字位于括号中或在其前后有一些特殊字符(例如 - _ " < etc.). I don't need them, I am looking for exact 10 letters or digits and have at least 1 digit in it. In the last line from the example there are 2 words with 10 characters - THINKCENTRE and 12Q6000AGE. THINKCENTRE doesn't contain a number, so it should be excluded.
输入 | 想要的结果 |
---|---|
军团T5 (90SV003WGE) | 90SV003WGE |
FLEX 5 (82R700BEGE) XKLUSIV | 82R700BEGE |
V17-IRU 83A2001NGE | 83A2001NGE |
联想 E16 G1 21JT000HGE_WIN 11 | 21JT000HGE |
联想 THINKCENTRE M70T 12Q6000AGE I7-12 | 12Q6000年龄 |
IDEAPAD 3 17ALC6 | 0 或空 |
不确定这是否可以通过公式实现,我尝试了不同的公式,但它给了我不同的结果。我设法找到一种方法来提取最长的单词,但通常这个数字不是最长的和/或包含大量需要清理的字符。
FILTERXML()
。对于 Excel-365 尝试-
=IFERROR(FILTERXML("<t><s>"&TEXTJOIN("</s><s>",1,TEXTSPLIT(A2,{"(",")","_"," "}))&"</s></t>","//s[translate(.,'1234567890','')!=.][string-length()=10]"),"")
适用于 Excel-2013 及更高版本。
=IFERROR(FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,"(",""),")",""),"_"," ")," ","</s><s>")&"</s></t>","//s[translate(.,'1234567890','')!=.][string-length()=10]"),"")
我几乎确信有一个聪明的正则表达式可以做到这一点,但我不太擅长 RegEx,所以我用普通的旧 VBA 来做。下面的函数相当简单。它需要一根绳子并且
Split
获取单个单词(
和 )
您可以在 Excel 工作表中将该函数用作 UDF
Function getProductNumber(s As String) As String
Dim tokens() As String, i As Long
tokens = Split(Replace(s, "_", " "), " ")
For i = 0 To UBound(tokens)
Dim word As String, j As Long, char As String
word = UCase(Replace(Replace(tokens(i), "(", ""), ")", ""))
If Len(word) = 10 Then
Dim digitFound As Boolean, charFound As Boolean
digitFound = False
charFound = False
For j = 1 To Len(word)
char = Mid(word, j, 1)
If Asc(char) >= Asc("0") And Asc(char) <= Asc("9") Then
digitFound = True
ElseIf Asc(char) >= Asc("A") And Asc(char) <= Asc("Z") Then
charFound = True
End If
Next
If digitFound And charFound Then
getProductNumber = word
Exit Function
End If
End If
Next i
End Function