【解決方法】文字列を任意の文字列にトリミングしたい

[ad_1]

助けてくれる人にお願いしたいのですが、以下のコードを含む URL アドレスが 1 つあり、文字列からトリミングしたいのです。

https://scontent

rid=289298631140853

、どうすればいいでしょうか? それについて助けてください。

私が試したこと:

<a href="https://scontent.xx.fbcdn.net/v/t39.25447-2/10000000_6431054706943551_3203837429521491984_n.mp4?_nc_cat=1&vs=9a2c9c3d6622741&_nc_vs=HBksFQAYJEdJQ1dtQUEtcGxhYkF0a1dBQkNvdXVBcVVYWXNibWRqQUFBRhUAAsgBABUAGCRHSk0xaGhWa3V4VWN1UFlBQUdvcWY0b2U4NEZ6YnY0R0FBQUYVAgLIAQBLB4gScHJvZ3Jlc3NpdmVfcmVjaXBlATENc3Vic2FtcGxlX2ZwcwAQdm1hZl9lbmFibGVfbnN1YgAgbWVhc3VyZV9vcmlnaW5hbF9yZXNvbHV0aW9uX3NzaW0AKGNvbXB1dGVfc3NpbV9vbmx5X2F0X29yaWdpbmFsX3Jlc29sdXRpb24AHXVzZV9sYW5jem9zX2Zvcl92cW1fdXBzY2FsaW5nABFkaXNhYmxlX3Bvc3RfcHZxcwAVACUAHIwXQAAAAAAAAAAREQAAACa8zIPgkNx0FQIoAkMzGAt2dHNfcHJldmlldxwXQILu5WBBiTcYIGRhc2hfdjRfNXNlY2dvcF9ocTJfZnJhZ18yX3ZpZGVvEgAYGHZpZGVvcy52dHMuY2FsbGJhY2sucHJvZDgSVklERU9fVklFV19SRVFVRVNUGwqIFW9lbV90YXJnZXRfZW5jb2RlX3RhZwZvZXBfaGQTb2VtX3JlcXVlc3RfdGltZV9tcwEwDG9lbV9jZmdfcnVsZQd1bm11dGVkE29lbV9yb2lfcmVhY2hfY291bnQGMTkwNTE0EW9lbV9pc19leHBlcmltZW50AAxvZW1fdmlkZW9faWQQMjc5MzEyOTkwNDE2MTk2OBJvZW1fdmlkZW9fYXNzZXRfaWQPMTA2MDM0OTkyNTczNzYzFW9lbV92aWRlb19yZXNvdXJjZV9pZA8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2dHNfcHJldmlldxwXQILu5WBBiTcYIGRhc2hfdjRfNXNlY2dvcF9ocTJfZnJhZ18yX3ZpZGVvEgAYGHZpZGVvcy52dHMuY2FsbGJhY2sucHJvZDgSVklERU9fVklFV19SRVFVRVNUGwqIFW9lbV90YXJnZXRfZW5jb2RlX3RhZwZvZXBfaGQTb2VtX3JlcXVlc3RfdGltZV9tcwEwDG9lbV9jZmdfcnVsZQd1bm11dGVkE29lbV9yb2lfcmVhY2hfY291bnQGMTkwNTE0EW9lbV9pc19leHBlcmltZW50AAxvZW1fdmlkZW9faWQQMjc5MzEyOTkwNDE2MTk2OBJvZW1fdmlkZW9fYXNzZXRfaWQPMTA2MDM0OTkyNTczNzYzFW9lbV92aWRlb19yZXNvdXJjZV9pZA8yNTY2Njk0OTM3ODUzNzQcb2VtX3NvdXJjZV92aWRlb19lbmNvZGluZ19pZA8zMDkxMTk2NDgxNDU2ODQOdnRzX3JlcXVlc3RfaWQAJQIcACXEARsHiAFzBDgxODMCY2QKMjAyMy0wNy0yMANyY2IGMTkwNTAwA2FwcAVWaWRlbwJjdBlDT05UQUlORURfUE9TVF9BVFRBQ0hNRU5UE29yaWdpbmFsX2R1cmF0aW9uX3MHNjA1Ljg4OAJ0cxVwcm9ncmVzc2l2ZV9lbmNvZGluZ3MA&ccb=1-7&_nc_sid=f97d39&efg=eyJ2ZW5jb2RlX3RhZyI6Im9lcF9oZCJ9&_nc_eui2=AeGdgPDzwjLaSGTONQoxW_5viTB_e75YaNCJMH97vlho0Mj4EgsT0Pr_E4RSt7Q3odM&_nc_ohc=S1DrBydfcBcAX8ZoN97&_nc_ht=scontent.fpnh10-1.fna&oh=00_AfB6TOrRCm9tACMC2Bxrx7H57ItjPcZgbJSjFL9yhIELSQ&oe=64BF38C8&_nc_rid=289298631140853</a>[<a href="https://scontent.xx.fbcdn.net/v/t39.25447-2/10000000_6431054706943551_3203837429521491984_n.mp4?_nc_cat=1&vs=9a2c9c3d6622741&_nc_vs=HBksFQAYJEd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A1Ljg4OAJ0cxVwcm9ncmVzc2l2ZV9lbmNvZGluZ3MA&ccb=1-7&_nc_sid=f97d39&efg=eyJ2ZW5jb2RlX3RhZyI6Im9lcF9oZCJ9&_nc_eui2=AeGdgPDzwjLaSGTONQoxW_5viTB_e75YaNCJMH97vlho0Mj4EgsT0Pr_E4RSt7Q3odM&_nc_ohc=S1DrBydfcBcAX8ZoN97&_nc_ht=scontent.fpnh10-1.fna&oh=00_AfB6TOrRCm9tACMC2Bxrx7H57ItjPcZgbJSjFL9yhIELSQ&oe=64BF38C8&_nc_rid=289298631140853" target="_blank" title="New Window">^</a>]

解決策 3

@OriginalGriff 解決策を拡張します…

URL を取得したら、URL の QueryString 部分をクリーンアップ/デコードしてから解析する必要があります。

1. デコード

VB
Dim rawUrl = "https://scontent.xx.fbcdn.net/v/t39.25447-2/10000000_6431054706943551_3203837429521491984_n.mp4?_nc_cat=1&vs=9a2c9c3d6622741&_nc_vs=HBksFQAYJEdJQ1dtQUEtcGxhYkF0a1dBQkNvdXVBcVVYWXNibWRqQUFBRhUAAsgBABUAGCRHSk0xaGhWa3V4VWN1UFlBQUdvcWY0b2U4NEZ6YnY0R0FBQUYVAgLIAQBLB4gScHJvZ3Jlc3NpdmVfcmVjaXBlATENc3Vic2FtcGxlX2ZwcwAQdm1hZl9lbmFibGVfbnN1YgAgbWVhc3VyZV9vcmlnaW5hbF9yZXNvbHV0aW9uX3NzaW0AKGNvbXB1dGVfc3NpbV9vbmx5X2F0X29yaWdpbmFsX3Jlc29sdXRpb24AHXVzZV9sYW5jem9zX2Zvcl92cW1fdXBzY2FsaW5nABFkaXNhYmxlX3Bvc3RfcHZxcwAVACUAHIwXQAAAAAAAAAAREQAAACa8zIPgkNx0FQIoAkMzGAt2dHNfcHJldmlldxwXQILu5WBBiTcYIGRhc2hfdjRfNXNlY2dvcF9ocTJfZnJhZ18yX3ZpZGVvEgAYGHZpZGVvcy52dHMuY2FsbGJhY2sucHJvZDgSVklERU9fVklFV19SRVFVRVNUGwqIFW9lbV90YXJnZXRfZW5jb2RlX3RhZwZvZXBfaGQTb2VtX3JlcXVlc3RfdGltZV9tcwEwDG9lbV9jZmdfcnVsZQd1bm11dGVkE29lbV9yb2lfcmVhY2hfY291bnQGMTkwNTE0EW9lbV9pc19leHBlcmltZW50AAxvZW1fdmlkZW9faWQQMjc5MzEyOTkwNDE2MTk2OBJvZW1fdmlkZW9fYXNzZXRfaWQPMTA2MDM0OTkyNTczNzYzFW9lbV92aWRlb19yZXNvdXJjZV9pZA8yNTY2Njk0OTM3ODUzNzQcb2VtX3NvdXJjZV92aWRlb19lbmNvZGluZ19pZA8zMDkxMTk2NDgxNDU2ODQOdnRzX3JlcXVlc3RfaWQAJQIcACXEARsHiAFzBDgxODMCY2QKMjAyMy0wNy0yMANyY2IGMTkwNTAwA2FwcAVWaWRlbwJjdBlDT05UQUlORURfUE9TVF9BVFRBQ0hNRU5UE29yaWdpbmFsX2R1cmF0aW9uX3MHNjA1Ljg4OAJ0cxVwcm9ncmVzc2l2ZV9lbmNvZGluZ3MA&ccb=1-7&_nc_sid=f97d39&efg=eyJ2ZW5jb2RlX3RhZyI6Im9lcF9oZCJ9&_nc_eui2=AeGdgPDzwjLaSGTONQoxW_5viTB_e75YaNCJMH97vlho0Mj4EgsT0Pr_E4RSt7Q3odM&_nc_ohc=S1DrBydfcBcAX8ZoN97&_nc_ht=scontent.fpnh10-1.fna&oh=00_AfB6TOrRCm9tACMC2Bxrx7H57ItjPcZgbJSjFL9yhIELSQ&oe=64BF38C8&_nc_rid=289298631140853"
Dim decodedUrl As String = WebUtility.HtmlDecode(rawUrl)

URL を手動で取得したので、何か作業する必要があります。

2.次にQueryStringを取得します。

VB
Dim uri = New Uri(decodedUrl)
Dim queryString = HttpUtility.ParseQueryString(uri.Query)

3. QueryString を取得したので、キーと値のペアに簡単にアクセスできます。

VB
For Each key As String In queryString.Keys
    Console.WriteLine($"{key} = {queryString.Item(key)}")
Next

そして出力は次のとおりです。

_nc_cat = 1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NRU5UE29yaWdpbmFsX2R1cmF0aW9uX3MHNjA1Ljg4OAJ0cxVwcm9ncmVzc2l2ZV9lbmNvZGluZ3MA
ccb = 1-7
_nc_sid = f97d39
efg = eyJ2ZW5jb2RlX3RhZyI6Im9lcF9oZCJ9
_nc_eui2 = AeGdgPDzwjLaSGTONQoxW_5viTB_e75YaNCJMH97vlho0Mj4EgsT0Pr_E4RSt7Q3odM
_nc_ohc = S1DrBydfcBcAX8ZoN97
_nc_ht = scontent.fpnh10-1.fna
oh = 00_AfB6TOrRCm9tACMC2Bxrx7H57ItjPcZgbJSjFL9yhIELSQ
oe = 64BF38C8
_nc_rid = 289298631140853

注: .NetFramework 4.7.1 コンソール アプリを作成し、 NuGet ギャラリー | NETStandard.Library 2.0.3[^] のための図書館 HttpUtility.ParseQueryString[^]。

完全なコードは次のとおりです。

VB.NET
Imports System.Net
Imports System.Web

Module Module1

    Sub Main()

        Dim rawUrl = "https://scontent.xx.fbcdn.net/v/t39.25447-2/10000000_6431054706943551_3203837429521491984_n.mp4?_nc_cat=1&vs=9a2c9c3d6622741&_nc_vs=HBksFQAYJEdJQ1dtQUEtcGxhYkF0a1dBQkNvdXVBcVVYWXNibWRqQUFBRhUAAsgBABUAGCRHSk0xaGhWa3V4VWN1UFlBQUdvcWY0b2U4NEZ6YnY0R0FBQUYVAgLIAQBLB4gScHJvZ3Jlc3NpdmVfcmVjaXBlATENc3Vic2FtcGxlX2ZwcwAQdm1hZl9lbmFibGVfbnN1YgAgbWVhc3VyZV9vcmlnaW5hbF9yZXNvbHV0aW9uX3NzaW0AKGNvbXB1dGVfc3NpbV9vbmx5X2F0X29yaWdpbmFsX3Jlc29sdXRpb24AHXVzZV9sYW5jem9zX2Zvcl92cW1fdXBzY2FsaW5nABFkaXNhYmxlX3Bvc3RfcHZxcwAVACUAHIwXQAAAAAAAAAAREQAAACa8zIPgkNx0FQIoAkMzGAt2dHNfcHJldmlldxwXQILu5WBBiTcYIGRhc2hfdjRfNXNlY2dvcF9ocTJfZnJhZ18yX3ZpZGVvEgAYGHZpZGVvcy52dHMuY2FsbGJhY2sucHJvZDgSVklERU9fVklFV19SRVFVRVNUGwqIFW9lbV90YXJnZXRfZW5jb2RlX3RhZwZvZXBfaGQTb2VtX3JlcXVlc3RfdGltZV9tcwEwDG9lbV9jZmdfcnVsZQd1bm11dGVkE29lbV9yb2lfcmVhY2hfY291bnQGMTkwNTE0EW9lbV9pc19leHBlcmltZW50AAxvZW1fdmlkZW9faWQQMjc5MzEyOTkwNDE2MTk2OBJvZW1fdmlkZW9fYXNzZXRfaWQPMTA2MDM0OTkyNTczNzYzFW9lbV92aWRlb19yZXNvdXJjZV9pZA8yNTY2Njk0OTM3ODUzNzQcb2VtX3NvdXJjZV92aWRlb19lbmNvZGluZ19pZA8zMDkxMTk2NDgxNDU2ODQOdnRzX3JlcXVlc3RfaWQAJQIcACXEARsHiAFzBDgxODMCY2QKMjAyMy0wNy0yMANyY2IGMTkwNTAwA2FwcAVWaWRlbwJjdBlDT05UQUlORURfUE9TVF9BVFRBQ0hNRU5UE29yaWdpbmFsX2R1cmF0aW9uX3MHNjA1Ljg4OAJ0cxVwcm9ncmVzc2l2ZV9lbmNvZGluZ3MA&ccb=1-7&_nc_sid=f97d39&efg=eyJ2ZW5jb2RlX3RhZyI6Im9lcF9oZCJ9&_nc_eui2=AeGdgPDzwjLaSGTONQoxW_5viTB_e75YaNCJMH97vlho0Mj4EgsT0Pr_E4RSt7Q3odM&_nc_ohc=S1DrBydfcBcAX8ZoN97&_nc_ht=scontent.fpnh10-1.fna&oh=00_AfB6TOrRCm9tACMC2Bxrx7H57ItjPcZgbJSjFL9yhIELSQ&oe=64BF38C8&_nc_rid=289298631140853"
        Dim decodedUrl As String = WebUtility.HtmlDecode(rawUrl)
        Dim uri = New Uri(decodedUrl)
        Dim queryString = HttpUtility.ParseQueryString(uri.Query)
        For Each key As String In queryString.Keys
            Console.WriteLine($"{key} = {queryString.Item(key)}")
        Next

    End Sub

End Module

解決策 1

必ず エキス 要求された部分文字列。 このようなタスクを達成する効果的な方法は、正規表現を使用することです。 見る .NET 正規表現 | Microsoft Learn[^]。

解決策 2

私は HtmlAgility Pack を使用して HTML ドキュメント全体を解析します。これにより、ページ上の特定のタグの個々の属性を抽出できます。
Visual Studio パッケージ マネージャーがこれを見つけてインストールします。また、その使用方法についてはネット上にたくさんあります。

[ad_2]

コメント

タイトルとURLをコピーしました