Powershell选择字符串以包含通配符

时间:2019-05-28 20:42:19

标签: regex powershell

我需要使用Select-String来查找已知字符串,然后是/$,然后是另一个已知字符串。

因此,假设文字字符串1为X,文字字符串2为Y,我该如何在powershell中查找字符串X/YX$Y

2 个答案:

答案 0 :(得分:0)

def form_valid(self, form):
    response = super().form_valid(form)
    existing_allowed_comments = self.request.get_signed_cookie('allowed_comments', default=None)
    if not self.request.user.is_authenticated:
        if existing_allowed_comments and str(self.object.pk) not in \
                existing_allowed_comments:
            response.set_signed_cookie('allowed_comments',
                 ", ".join([existing_allowed_comments, str(self.object.pk)])
        elif not existing_allowed_comments:
            response.set_signed_cookie('allowed_comments', self.object.pk
    return response

答案 1 :(得分:0)

即使字符串很简单,我也会复合RegEx:

## Q:\Test\2019\05\28\SO_56349782.ps1

$String1 = [regex]::Escape("X")
$String2 = [regex]::Escape("Y")
$RegEx   = [regex]"$String1(\/|\$)$String2"

$Text = @'
foo X/Y 
foo X$Y baz
foo X/Y bar X$Y baz
'@ -split '\r?\n'

$Text | Select-String -Pattern $RegEx -AllMatches

($Text | Select-String -Pattern $RegEx -AllMatches).Matches.Value

示例输出:

foo X/Y
foo X$Y baz
foo X/Y bar X$Y baz

X/Y
X$Y
X/Y
X$Y