を解析するために、およびWebページからvba
組み合わせてスクリプトを作成しました。スクリプトを実行すると、これらの情報を完璧に取得できます。しかし、事は、私は別の3を使用しましたで、それが正常に私は、3つの異なるを作成行く作ることのように、、、と。regular expressions
company name
phone
fax
expressions
regex objects
rxp
rxp1
rxp2
私の質問:以下で行ったのとは異なり、regex object
3つを使用できるものを作成するにはどうすればよいpatterns
ですか?
これはスクリプトです(動作するもの):
Sub GetInfo()
Const Url$ = "https://www.austrade.gov.au/SupplierDetails.aspx?ORGID=ORG0120000508&folderid=1736"
Dim rxp As New RegExp, rxp1 As New RegExp, rxp2 As New RegExp
With New XMLHTTP60
.Open "GET", Url, False
.send
rxp.Pattern = "Company Name:(\s[\w\s]+)"
rxp1.Pattern = "Phone:(\s\+[\d\s]+)"
rxp2.Pattern = "Fax:(\s\+[\d\s]+)"
If rxp.Execute(.responseText).Count > 0 Then
[A1] = rxp.Execute(.responseText).Item(0).SubMatches(0)
End If
If rxp1.Execute(.responseText).Count > 0 Then
[B1] = rxp1.Execute(.responseText).Item(0).SubMatches(0)
End If
If rxp2.Execute(.responseText).Count > 0 Then
[C1] = rxp2.Execute(.responseText).Item(0).SubMatches(0)
End If
End With
End Sub
上記のスクリプトを実行するためにライブラリに追加する参照:
Microsoft XML, v6.0
Microsoft VBScript Regular Expressions
代替で正規表現を作成しrxp.Global = True
、とのグローバルマッチングを有効にして、既知の文字列をグループ1にキャプチャし、それらの未知の部分をグループ2にキャプチャすることができます。次に、グループ1の値を確認することにより、変数に正しい値を割り当てることができます。 :
Const Url$ = "https://www.austrade.gov.au/SupplierDetails.aspx?ORGID=ORG0120000508&folderid=1736"
Dim rxp As New RegExp
Dim ms As MatchCollection
Dim m As Match
Dim cname As String, phone As String, fax As String
With New XMLHTTP60
.Open "GET", Url, False
.send
rxp.Pattern = "(Phone|Company Name|Fax):\s*(\+?[\w\s]*\w)"
rxp.Global = True
Set ms = rxp.Execute(.responseText)
For Each m In ms
If m.SubMatches(0) = "Company Name" Then cname = m.SubMatches(1)
If m.SubMatches(0) = "Phone" Then phone = m.SubMatches(1)
If m.SubMatches(0) = "Fax" Then fax = m.SubMatches(1)
Next
Debug.Print cname, phone, fax
End With
出力:
Vaucraft Braford Stud +61 7 4942 4859 +61 7 4942 0618
正規表現のデモを参照してください。
パターンの詳細:
(Phone|Company Name|Fax)
-キャプチャグループ1:3つの選択肢のいずれか:\s*
-コロンと0以上の空白(\+?[\w\s]*\w)
-キャプチャグループ2:
\+?
-オプション +
[\w\s]*
-0つ以上の文字、数字、_
または空白\w
-1文字、数字、または_
。この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加