Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要修剪powershell脚本中的url_Powershell_Url - Fatal编程技术网

需要修剪powershell脚本中的url

需要修剪powershell脚本中的url,powershell,url,Powershell,Url,我有一个URLwww.example.com:1234/,我需要将上面的内容精简为两个变量: example.com 00234 端口的第一位数字将替换为00 这可以在PowerShell中实现吗?这里有一种方法。。。[咧嘴笑] [uri]$url = 'www.example.com:1234/' $Value1 = ($url.Scheme).Replace('www.','') $Value2 = "00" + ($url.AbsolutePath).Substring(1).Tri

我有一个URL
www.example.com:1234/
,我需要将上面的内容精简为两个变量:

  • example.com
  • 00234
    • 端口的第一位数字将替换为
      00

  • 这可以在PowerShell中实现吗?

    这里有一种方法。。。[咧嘴笑]

    [uri]$url = 'www.example.com:1234/'
    
    $Value1 = ($url.Scheme).Replace('www.','')
    $Value2 = "00" + ($url.AbsolutePath).Substring(1).TrimEnd('/')
    
    注释掉或删除任何不需要的属性。[咧嘴笑]


    根据请求,简化版本。。。[咧嘴笑]

    希望有帮助,

    lee

    提供以下改进:


    良好的ole rfc2606;)
    # fake reading in a list of URLs
    #    in real life, use Get-Content
    $UrlList = @'
    www.example.com:1234/
    www3.example.net:9876
    www.other.example.org:5678/
    '@ -split [environment]::NewLine
    
    $Regex = '^www.*?\.(?<Domain>.+):(?<Port>\d{1,}).*$'
    
    $Results = foreach ($UL_Item in $UrlList)
        {
        $Null = $UL_Item -match $Regex
    
        [PSCustomObject]@{
            URL = $UL_Item
            Domain = $Matches.Domain
            OriginalPort = $Matches.Port
            Port = '00{0}' -f (-join $Matches.Port.ToString().SubString(1))
            }
        }
    
    $Results
    
    URL                        Domain           OriginalPort Port 
    ---                        ------           ------------ ---- 
    www.example.com:1234/     example.com     1234         00234
    www3.example.net:9876      example.net      9876         00876
    www.other.example.org:5678/ other.example.org 5678         00678    
    
    $UserInput = 'www.example.com:1234/'
    
    $Regex = '^www.*?\.(?<Domain>.+):(?<Port>\d{1,}).*$'
    
    $Null = $UserInput -match $Regex
    
    $Domain = $Matches.Domain
    $Port = '00{0}' -f (-join $Matches.Port.SubString(1))
    
    $Domain
    $Port
    
    example.com
    00234
    
    # Input URL string
    $urlText = 'www.example.com:1234/'
    
    # Prepend 'http://' and cast to [uri] (System.Uri), which
    # parses the URL string into its constituent components.
    $urlObj = [uri] "http://$urlText"
    
    # Extract the information of interest
    $domain = $urlObj.Host -replace '^www\.' # -> 'example.com'
    $modifiedPort = '00' + $urlObj.Port.ToString().Substring(1) # -> '00234'