从评论中提取电话号码

  • 我有专栏评论(超过5000个案例)。
  • 这些评论有文字,数字,date,一切。
  • 我需要从这些评论中获得电话号码。
  • 电话号码随机的地方为每个评论,所以LEFT ,中或RIGHT将无法正常工作

我已经达到的最接近的结果是与Kutools =EXTRAXTNUMBERS() ……但我得到一行数字,其中包括date,ID`s等

宁愿一个公式。 🙂

下面的两个样本注释,所需的电话号码以粗体显示

2017年2月2日星期四2:37 PM协调世界时.3868 67076939 ,pers.pārv.Tatjana呼叫结果select:Norunacitālaikā – 2017-02-03 07:15星期三,2017年2月8日上午8:18 Coordinated Universal时间.3868 nr。 67074071- neeksistē,personāladaļasvad.Tatjana neatbild,arīnr。 67076600通话结果select:2017年2月10日星期五上午7:15协调通用时间* .3868 *** piezv ap 13通话结果select:Norunacitālaikā – 2017-02-10 11:15

2017年2月2日星期四11:15 AM协调世界时4213zvanīt66119908通话结果select:Norunacitālaikā – 2017-02-07 09:00 2017年2月14日星期二下午12:59协调世界时.4532 * anita @ dzintarniece@rtp.lv通话结果select:Turpinātinternetā

这个小的UDF()将返回一个string中的所有8位数字子string:

 Public Function PHNum(s As String) As String Dim L As Long, i As Long, temp As String Dim CH As String L = Len(s) temp = "" PHNum = "" For i = 1 To L CH = Mid(s, i, 1) If IsNumeric(CH) Then temp = temp & CH If Len(temp) = 8 Then PHNum = PHNum & vbCrLf & temp End If Else temp = "" End If Next i End Function 

在这里输入图像描述

注意:

要在输出单元格中获取堆叠格式,请将其格式化以包装。

正则expression式解决scheme

这个UDF从文本中提取电话号码作为数组。 您最终可以使用Join将其转换为csvstring,也可以将该数组粘贴到一系列单元格中。

 Function extractPhones(s As String) As String() Dim i As Long, matches, match, ret With CreateObject("VBScript.Regexp") .Global = True .Pattern = "\W[26]\d{7}\W" Set matches = .Execute(s) End With ReDim ret(1 To matches.Count) As String For Each match In matches i = i + 1 ret(i) = Mid(match, 2, Len(match) - 2) Next extractPhones = ret End Function 

它使用正则expression式来匹配电话号码与这些规格:

  • 正好是8位数字
  • 由6或2开始
  • 不是在字母数字字母之前或之后,而是由空白或标点字符。

演示

使用UDF,您可以使用以下代码来完成此操作:

要使用它:

  1. ALT + F11
  2. 插入模块
  3. 粘贴代码
  4. 在Excel工作表中,使用此公式=get_phone("CELL_WITH_NUMBER_HERE")获取单元格中第一个8位数字的序列。

码:

 Public Function get_phone(cell As Range) Dim s As String Dim i As Integer Dim num Dim counter As Integer 'get cell value s = cell.Value 'set the counter counter = 0 'loop through the entire string For i = 1 To Len(s) 'check to see if the character is a numeric one If IsNumeric(Mid(s, i, 1)) = True Then 'add it to the number num = num + Mid(s, i, 1) counter = counter + 1 'check if we've reached 8 digits If counter = 8 Then get_phone = num Exit Function End If Else 'was not numeric so reset counter and answer counter = 0 num = "" End If Next i End Function 

示例图像:

在这里输入图像描述

另一个正则expression式选项,将所有匹配返回给单个单元格

在这里输入图像描述

请参阅https://regex101.com/r/Hdv65h/1

 Function StrPhone(strIn As String) As String Dim objRegexp As Object Set objRegexp = CreateObject("VBScript.Regexp") With objRegexp .Global = True .Pattern = ".*?(\d{8})|.*$" StrPhone = Trim(.Replace(strIn, "$1 ")) End With End Function 

Excel中有一个附加组件,用于正则expression式( http://seotoolsforexcel.com/regexpfind/ )。 在你的情况下,它可能会很复杂,因为你不知道一个电话号码将出现在你的手机多less次。 对于这些情况,我build议你使用其他用户提供的VBA脚本。