regex - URL中作为参数传递的特殊字符

标签 regex powershell special-characters

要求:需要一种方法来处理特殊字符,例如%&。需要调整以下代码,以便按原样处理通过$Control文件传入的特殊字符。

例如:我在$control文件中有一个条目25% Dextrose(25ml)。我需要一种方法,以便$ie.Navigate应该简单地导航到https://www.xxxy.com/search/all?name=25% Dextrose(25ml)。当前,它被路由到https://www.xxxy.com/search/all?name=25%% Dextrose(25ml)(注意URL中的额外%),因此找不到该网页。

**Few examples of special characters that need to be tackled:** 
'/' - 32care Mouth/Throat
'%' - 3d1% Gel(30g)
'&' - Accustix Glucose & Protein
'/' - Ace Revelol(25/(2.5mg)


function getStringMatch
     {
        # Loop through all 2 digit combinations in the $path directory
        foreach ($control In $controls)
        {
            $ie = New-Object -COMObject InternetExplorer.Application
            $ie.visible = $true
            $site = $ie.Navigate("https://www.xxxy.com/search/all?name=$control")
            $ie.ReadyState

            while ($ie.Busy -and $ie.ReadyState -ne 4){ sleep -Milliseconds 100 }

            $link = $null
            $link = $ie.Document.get_links() | where-object {$_.innerText -eq "$control"}
            $link.click()

            while ($ie.Busy -and $ie.ReadyState -ne 4){ sleep -Milliseconds 100 }

           $ie2 = (New-Object -COM 'Shell.Application').Windows() | ? {
           $_.Name -eq 'Windows Internet Explorer' -and $_.LocationName -match "^$control"
           }

            # NEED outerHTML of new page. CURRENTLY it is working for some.

            $ie.Document.body.outerHTML > d:\med$control.txt
        }
    }

    $controls = "Sporanox"

    getStringMatch

最佳答案

您要对URI进行URL编码。首先添加:

Add-Type -AssemblyName 'System.Web'

然后像这样编码URL:
$controlUri = [System.Web.HttpUtility]::UrlEncode($control)
$site = $ie.Navigate("https://www.xxxy.com/search/all?name=$controlUri")

关于regex - URL中作为参数传递的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27887655/

相关文章:

PowerShell 脚本 ffmpeg

从字符串中提取数字的正则表达式 (gwmi Win32_OperatingSystem).Name

powershell - 获取列表中每台计算机的序列号(Powershell)

oauth-2.0 - OAuth2 访问 token 中允许使用哪些字符?

python - 在 python 中制作一个包含\x 的字符串

regex - Bash 匹配字符串与正则表达式

java - 简单的java正则表达式替换问题

regex - 如何在 awk 或 sed 中使用正则表达式来查找 DNA 序列中的所有同聚物?

arrays - 循环遍历文件名列表并迭代变量/数组,使用 bash 从文件名中删除所有字符串

C# - 如何使用正则表达式替换 NULL 字符?