Home > Blockchain >  How to trim expression with wildcard in script?
How to trim expression with wildcard in script?

Time:11-24

I have a script that parses a URL. If the query contains the user and the password, it will retrieve this.

I would therefore like to keep the PHP query if necessary.


      Add-Type -AssemblyName System.Web
    
    $url = "http://toto.site:8080/user/password12349876toolong/2716?checkedby:toto.net"
    
    
    $uri = [System.Uri]$url
    $query = $uri.Query
    $IPTVhost = $uri.Host
    $IPTVport = $uri.Port
    $Segments = $uri.Segments.TrimEnd('/')
    $proto = $uri.Scheme
    
    $ParsedQueryString = [System.Web.HttpUtility]::ParseQueryString($uri.Query)
    
    if(!$query) {
    
    if ($Segments[1].TrimEnd('/') -eq "live"){
    
    
    $username = $Segments[2].TrimEnd('/')
    
    $password = $Segments[3].TrimEnd('/')
    }
    else
    {
    
    $username = $Segments[1].TrimEnd('/')
    $password = $Segments[2].TrimEnd('/')
    }
    }
    
    
    else {
    $username = $ParsedQueryString['username']
    $password = $ParsedQueryString['password']
    }
    cls
    if (!($url -like '\?checkedby\:'))
    {
    echo "this is a chekedby url"
    $filter = $url.TrimEnd("\?checkedby\:toto.net")
    echo "filter : $filter"
    }
    echo "url: $url"
    echo "query: $query"
    echo "host: $IPTVhost"
    echo "port: $IPTVport"
    echo "segment: $Segments"
    echo "proto: $proto"
    echo "username: $username"
    echo "password: $password"

I would like to filter a string of characters when I enter a URL in my script (which is often found in the query but not always). I know it always starts with "? Checkedby:" or "& checkedby:" and is always at the end of the url.

Problematic: the chain is variable, it can be:

http://toto.com:8080/get.php?username=toto&password=toto&checkedby:titi.com

or

http://toto.com/1234/4321/5678?checkedby:anyone.xyz

or

http://toto.com/1234/4321/5678?master.m3u8&checkedby:anyelse.to

or this crap :

http://toto.com:8080/get.php?username=toto&password=toto&type=output.ext?checkedby:titi.com

I have tried several methods with TrimEnd but nothing helps. The only thing that works is an exact expression like:

$filter = $url.TrimEnd("\?checkedby\:toto.net")

but that doesn't work (and that's normal) with a url that ends in:

&checkedby:another.com.

So, question :

How to remove everything that starts with:

&checkedby:

or

?checkedby:

Thank you.

CodePudding user response:

Building on Santiago Squarzon 's helpful comment:

Use a regex-based operation via the -replace operator:

'http://toto.com:8080/get.php?username=toto&password=toto&checkedby:titi.com',
'http://toto.com/1234/4321/5678?checkedby:anyone.xyz',
'http://toto.com/1234/4321/5678?master.m3u8&checkedby:anyelse.to',
'http://toto.com:8080/get.php?username=toto&password=toto&type=output.ext?checkedby:titi.com' |
  ForEach-Object { $_ -replace '[?&]checkedby:. ' }

For an explanation of the regex and the option to experiment with it, see this regex101.com page.

  • Related