gpt4 book ai didi

powershell - 如何在脚本中使用通配符修剪表达式?

转载 作者:行者123 更新时间:2023-12-05 04:41:26 26 4
gpt4 key购买 nike

我有一个解析 URL 的脚本。如果查询包含用户和密码,它将检索此内容。

因此,如有必要,我想保留 PHP 查询。


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"

我想在我的脚本中输入一个 URL 时过滤一串字符(这在查询中经常出现,但并非总是如此)。我知道它总是以 "? Checkedby:""& checkedby:" 开头,并且总是在 url 的末尾。

有问题:链是可变的,它可以是:

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

我已经用 TrimEnd 尝试了几种方法,但没有任何帮助。唯一有效的是像这样的精确表达式:

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

但这对于以以下结尾的 url 不起作用(这很正常):

&checkedby:another.com.

所以,问题:

如何删除以以下开头的所有内容:

&checkedby:

?checkedby:

谢谢。

最佳答案

基于 Santiago Squarzon的有用评论:

使用 regex通过 -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:.+' }

有关正则表达式的解释和试验它的选项,请参阅 this regex101.com page .

关于powershell - 如何在脚本中使用通配符修剪表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70089986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com