返回列表 发帖

[问题求助] 正则匹配url第一个反斜杠内容

比如 https://agc.qq.com/show/chan

匹配出show结果

本帖最后由 zhanglei1371 于 2017-10-16 22:47 编辑

这个?
http.*?//[^/]+/([^/]+)/.*$COPY

TOP

Option Explicit
Test
'************************************************************************
Sub Test()
'************************************************************************
    Dim str, strPattern
    str = "https://agc.qq.com/show/chan"
    strPattern = "http[s]{0,1}:\/\/[0-9A-z\.]+?\/([^\/]+)?[\/]{0,1}?.*"
    MsgBox GetRE_SM0(str, strPattern)
End Sub
'************************************************************************
'正則表達式:取得字符串的第一個子匹配項目
'************************************************************************
Function GetRE_SM0(ByVal str, ByVal strPattern)
    Dim oRE
    Set oRE = CreateObject("vbScript.regExp")
    oRE.Pattern = strPattern
    If oRE.Test(str) Then
        If oRE.Execute(str)(0).SubMatches.Count >= 1 Then
            GetRE_SM0 = oRE.Execute(str)(0).SubMatches(0)
        End If
    End If
    Set oRE = Nothing
End FunctionCOPY
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

本帖最后由 523066680 于 2017-10-18 16:11 编辑

反斜杠不是 \ 吗
echo https://agc.qq.com/show/chan|perl -e "print [split(/\/+/,<>)]->[2]"COPY
还是不要用切割了,匹配大概是这样吧:
$s = 'https://agc.qq.com/show/chan';
$s =~ /\/{2}.*?\/([^\/]+)/;
print $1;COPY
[url=][/url]

TOP

(?!/).*/([^/]+)/.*COPY
第一个捕获组捕获到的就是
1

评分人数

人生是一部书/只有这一页最温暖/读懂它的时候/我们在远方流泪...

TOP

返回列表