Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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
Regex PowerShell正则表达式-带通配符和逗号的字_Regex_Powershell - Fatal编程技术网

Regex PowerShell正则表达式-带通配符和逗号的字

Regex PowerShell正则表达式-带通配符和逗号的字,regex,powershell,Regex,Powershell,我理解这是一个简单的操作,但却遇到了麻烦 我可以用逗号替换一个单词的结尾: $firstval = 'ssonp,RDPNP,LanmanWorkstation,webclient,MfeEpePcNP,PRNetworkProvider' ($firstval) -replace 'webclient+,','' ssonp、RDPNP、LanmanWorkstation、MfeEpePcNP、PRNetworkProvider 但是,我还无法解决如何在单词中添加通配符,或者如何让多个单词

我理解这是一个简单的操作,但却遇到了麻烦

我可以用逗号替换一个单词的结尾:

$firstval = 'ssonp,RDPNP,LanmanWorkstation,webclient,MfeEpePcNP,PRNetworkProvider'

($firstval) -replace 'webclient+,',''
ssonp、RDPNP、LanmanWorkstation、MfeEpePcNP、PRNetworkProvider

但是,我还无法解决如何在单词中添加通配符,或者如何让多个单词以逗号开头,例如:

w*客户+,*费用*等

(添加空格以停止在问题中被解释为格式)


玩了一些渗透,并尝试使用其他问题中的示例,但没有任何运气。

操作符
-replace
将正则表达式作为其第一个参数。您似乎混淆了通配符和正则表达式。您的模式
w*client+,*fee*,
,虽然是有效的正则表达式,但似乎打算使用通配符

*
通配符等价的正则表达式是
*
,其中
表示“任何字符”,而
*
表示“0次或多次出现”。因此,等价于
w*client,
的正则表达式将是
w*client,
,类似地,等价于
*fee*,
的正则表达式将是
*fee*,
。但是,由于要搜索的字符串具有逗号分隔的值,因此我们不希望模式包含“任何字符”(
*
),而是包含“除逗号以外的任何字符”(
[^,]*
)。因此,要使用的模式分别成为
w[^、]*客户机、
[^、]*费用[^、]*、

要搜索字符串中的两个单词,请使用
|
分隔这两种模式。以下内容构建了这样一个模式,并针对不同位置的匹配字符串对其进行测试:

# Match w*client or *fee*
$wordPattern = 'w[^,]*client|[^,]*fee[^,]*';
# Match $wordPattern and at most one comma before or after
$wordWithAdjacentCommaPattern = '({0}),?|,({0})$' -f $wordPattern;

"`$wordWithAdjacentCommaPattern: $wordWithAdjacentCommaPattern";
# Replace single value
'webclient', `
# Replace first value
'webclient,middle,last', `
# Replace middle value
'first,webclient,last', `
# Replace last value
'first,middle,webclient' `
    | ForEach-Object -Process { '"{0}" => "{1}"' -f $_, ($_ -replace $wordWithAdjacentCommaPattern); };
这将产生以下结果:

$wordWithAdjacentCommaPattern: (w[^,]*client|[^,]*fee[^,]*),?|,(w[^,]*client|[^,]*fee[^,]*)$
"webclient" => ""
"webclient,middle,last" => "middle,last"
"first,webclient,last" => "first,last"
"first,middle,webclient" => "first,middle"

一个非正则表达式,您可以考虑将输入字符串分割成单独的值,筛选出与某些通配符匹配的值,并将剩下的值重新组合为逗号分隔值:

(
    'ssonp,RDPNP,LanmanWorkstation,webclient,MfeEpePcNP,PRNetworkProvider' -split ',', -1, 'SimpleMatch' `
        | Where-Object { $_ -notlike 'w*client' -and $_ -notlike '*fee*'; } `
) -join ',';

顺便说一下,您使用正则表达式
webclient+,
匹配并从字符串中删除文本
webclient,
(看起来像
HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\Control\NetworkProvider\Order\ProviderOrder
注册表值)。请注意,使用
+
,将搜索文本
网络链接
,然后搜索一个或多个出现的
t
,然后搜索文本
。因此,这将匹配
webclientt、
webclientt、
webclienttttttt、
,等等,以及
webclient、
。如果您只想匹配
webclient,
,那么您可以使用模式
webclient,
(no
+
)。

-replace
操作符将正则表达式作为其第一个参数。您似乎混淆了通配符和正则表达式。您的模式
w*client+,*fee*,
,虽然是有效的正则表达式,但似乎打算使用通配符

*
通配符等价的正则表达式是
*
,其中
表示“任何字符”,而
*
表示“0次或多次出现”。因此,等价于
w*client,
的正则表达式将是
w*client,
,类似地,等价于
*fee*,
的正则表达式将是
*fee*,
。但是,由于要搜索的字符串具有逗号分隔的值,因此我们不希望模式包含“任何字符”(
*
),而是包含“除逗号以外的任何字符”(
[^,]*
)。因此,要使用的模式分别成为
w[^、]*客户机、
[^、]*费用[^、]*、

要搜索字符串中的两个单词,请使用
|
分隔这两种模式。以下内容构建了这样一个模式,并针对不同位置的匹配字符串对其进行测试:

# Match w*client or *fee*
$wordPattern = 'w[^,]*client|[^,]*fee[^,]*';
# Match $wordPattern and at most one comma before or after
$wordWithAdjacentCommaPattern = '({0}),?|,({0})$' -f $wordPattern;

"`$wordWithAdjacentCommaPattern: $wordWithAdjacentCommaPattern";
# Replace single value
'webclient', `
# Replace first value
'webclient,middle,last', `
# Replace middle value
'first,webclient,last', `
# Replace last value
'first,middle,webclient' `
    | ForEach-Object -Process { '"{0}" => "{1}"' -f $_, ($_ -replace $wordWithAdjacentCommaPattern); };
这将产生以下结果:

$wordWithAdjacentCommaPattern: (w[^,]*client|[^,]*fee[^,]*),?|,(w[^,]*client|[^,]*fee[^,]*)$
"webclient" => ""
"webclient,middle,last" => "middle,last"
"first,webclient,last" => "first,last"
"first,middle,webclient" => "first,middle"

一个非正则表达式,您可以考虑将输入字符串分割成单独的值,筛选出与某些通配符匹配的值,并将剩下的值重新组合为逗号分隔值:

(
    'ssonp,RDPNP,LanmanWorkstation,webclient,MfeEpePcNP,PRNetworkProvider' -split ',', -1, 'SimpleMatch' `
        | Where-Object { $_ -notlike 'w*client' -and $_ -notlike '*fee*'; } `
) -join ',';

顺便说一下,您使用正则表达式
webclient+,
匹配并从字符串中删除文本
webclient,
(看起来像
HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\Control\NetworkProvider\Order\ProviderOrder
注册表值)。请注意,使用
+
,将搜索文本
网络链接
,然后搜索一个或多个出现的
t
,然后搜索文本
。因此,这将匹配
webclientt、
webclientt、
webclienttttttt、
,等等,以及
webclient、
。如果您只对匹配
webclient,
感兴趣,那么您可以使用模式
webclient,
(no
+
)。

谢谢,这正是我要找的,是的,这就是我要找的注册表项。如果我想在字符串中的任何位置查找这两个单词,如何在输入字符串中搜索和替换这两个单词,请参阅我的编辑。值得一提的是,
-split
也是一个正则表达式operator@MathiasR.Jessen说得好。我本想包括
-split
SimpleMatch
选项,但没有<代码>'ssonp,RDPNP,LanmanWorkstation,webclient,MfeEpePcNP,PRNetworkProvider'.Split(',')将是另一个真正的非正则表达式替代品。非常感谢,有很多非常有用的信息。谢谢。这正是我要找的,是我要找的注册表项。如果我想在字符串中的任何位置查找这两个单词,如何在输入字符串中搜索和替换这两个单词,请参阅我的编辑。值得一提的是,
-split
也是一个正则表达式operator@Mathias