Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何在Excel的URL中获取介于2/之间的数字_Regex_Excel_Vba - Fatal编程技术网

Regex 如何在Excel的URL中获取介于2/之间的数字

Regex 如何在Excel的URL中获取介于2/之间的数字,regex,excel,vba,Regex,Excel,Vba,我试图在URL中提取4、5或6个数字。这个数字在URL中并不总是出现在同一个位置,有时它们位于第四位,有时位于第五位,有时,它们后面有一堆狼吞虎咽的东西。下面是一些例子 我有一些VBA,我从一个黑客一起,但正如你所看到的,它在一些网址(上图)上崩溃。我如何修改它,或者是否有一个公式可以用于始终返回右列中突出显示的数字 Public Function listNum(Myrange As Range) As String Dim regEx As Object

我试图在URL中提取4、5或6个数字。这个数字在URL中并不总是出现在同一个位置,有时它们位于第四位,有时位于第五位,有时,它们后面有一堆狼吞虎咽的东西。下面是一些例子

我有一些VBA,我从一个黑客一起,但正如你所看到的,它在一些网址(上图)上崩溃。我如何修改它,或者是否有一个公式可以用于始终返回右列中突出显示的数字

Public Function listNum(Myrange As Range) As String
    Dim regEx           As Object
    Dim inputMatches    As Object
    Dim regExString     As String
    Dim strInput        As String
    Dim a               As Byte

    Set regEx = CreateObject("VBScript.RegExp")

    With regEx
        .Pattern = "([0-9]{3,7})"
        .IgnoreCase = True
        .Global = True
        s = Myrange.Value
        Set inputMatches = .Execute(s)

        If regEx.Test(s) Then
            listNum = .Replace(s, "~")
            a = InStr(1, listNum, "~", vbTextCompare)
            listNum = Mid(s, a, Len(s) - (Len(listNum) - 1))
        Else
            listNum = ""
            'listNum = s 'takes entire contents of cell and puts it in, we do not want that
        End If
    End With
End Function
更新:显然,它们并不总是在两个斜杠之间,但看起来它们总是5个字符,这里还有两个URL。我们又开始营业了

/listings/?action=display&listingid=31221 /es-gl/listings/?action=display&listingid=30931&menuid=706&hit=1 /清单/?操作=显示和列表ID=31221 /es gl/listings/?操作=显示和列表ID=30931&menuid=706&hit=1
数字位于字符串的末尾和后面
/?
。因此:


Regex
(?如果您要查找数字,为什么不将两个正斜杠之间的数字作为捕获组进行匹配,然后提取该组

Set re = New RegExp
re.Pattern = "/(\d{3,7})/"

For Each m In re.Execute(s)
    listNum = m.Submatches(0)
Next

作为一个公式解决方案,这应该适用于您:

=--MID(A1,MATCH(TRUE,INDEX(ISNUMBER(--MID(SUBSTITUTE(A1,"-","|"),ROW(INDIRECT("1:"&LEN(A1)-4)),5)),),0),5)
不带正则表达式:

Public Function GetNumber(s As String) As String
    Dim a, L As Long
    ary = Split(s, "/")
    For Each a In ary
        L = Len(a)
        If L > 3 And L < 7 And IsNumeric(a) Then
            GetNumber = a
            Exit Function
        End If
    Next a
    GetNumber = ""
End Function
公共函数GetNumber(s作为字符串)作为字符串
暗淡的a,我也一样长
ary=拆分(“/”)
每一个都是一个单位
L=Len(a)
如果L>3且L<7且为数字(a),则
GetNumber=a
退出功能
如果结束
下一个
GetNumber=“”
端函数


将提取4、5或6位数字。

数字是否始终为5位(不多也不少)?我想是的,但如果可能的话,我想找4或6以防万一,但是如果你有一个基于这些例子的5的解决方案,正则表达式并不太难:不幸的是,我只知道JavaScript的正则表达式,所以它可能需要适应你的语言。使用
.Pattern=“/([0-9]{3,7})/”
并抓取
匹配.SubMatches.Item(0)
“/”
上进行简单拆分,并在返回的项目上使用
IsNumeric()
,也许
Len()
就足够了。就是这样!你太快了,我甚至还不能将其标记为正确。给我8分钟的时间,正面展望是
(?=…)
,而不是
(?因为它不总是在同一个位置,我现在必须再次找到listingid=Too,你是大师