regex 模式来提取字符串中URL的所有匹配项

o0lyfsai  于 2023-06-07  发布在  其他
关注(0)|答案(1)|浏览(355)

我在字符串中有一些像这样的URL http://app14.co.ad.local:90/ATT\me1111419.org
.和反斜杠\或正斜杠/的数量不是恒定的。
URL以"http“开头,以点+结尾(“org”,“com”,“net”)
| 弦|预期结果|
| - -----|- -----|
| http://app14.co.ad.local:90/ATT\me1111419.org blasss test wwwhttp://app14.co.ad.local:90/ATT\me1.111.com xxxxbb aaa qwer fff http://app14.co.ad.local:90/ATT\bbb1419.net www| http://app14.co.ad.local:90/ATT\me1111419.org http://app14.co.ad.local:90/ATT\me1.111.com http://app14.co.ad.local:90/ATT\b.bb1.419.net |
下面的代码将正确工作,只有当我的字符串只包含URL,没有其他的话。
我对图案本身的问题。

Option Explicit
Option Compare Text

Function RegexMatches(strInput As String) As String

    Dim re As New RegExp
    Dim rMatch As Object, s As String, arrayMatches(), i As Long

     With re
       .Pattern = "(http:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?"
       .Global = True
       .MultiLine = True
       .IgnoreCase = True
     End With
    
     If re.test(strInput) Then
        For Each rMatch In re.Execute(strInput)
            ReDim Preserve arrayMatches(i)
            arrayMatches(i) = rMatch.Value
            i = i + 1
        Next
     End If
    
    RegexMatches = Join(arrayMatches, vbLf)

End Function
vjrehmav

vjrehmav1#

您可以用途:

http://            # Match 'http://'
\S+                # followed by one or more non-whitespace characters
\.(?:net|com|org)  # then either '.net', '.com' or '.org'.

试试on regex101.com

相关问题