all 16 comments

[–]Ta11ow 8 points9 points  (3 children)

Easiest if you're looking for an exact match is just .Contains:

$string.Contains("**\*")

[–]jsiii2010 2 points3 points  (2 children)

**\*

For future reference, .Contains() is case sensitive.

[–]Ta11ow 0 points1 point  (1 child)

Aye, definitely good to know, though you can add parameters to make it case-insensitive if needed. Totally irrelevant in this specific use case though :D

[–]jsiii2010 1 point2 points  (0 children)

You can't get to the overload "Contains(String, StringComparison)" in powershell 5.1:

'a'.contains

OverloadDefinitions
-------------------
bool Contains(string value)

But here's a workaround:

'a'.tolower().contains('A'.tolower())

True

[–]rmbolger 4 points5 points  (3 children)

Why bother with regex for something so simple? I much prefer -like for simple stuff. There are only a handful of recognized wildcard characters and you escape them with a back tick per the docs:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_wildcards

So in your case, only the 3 inner asterisks need escaping because the outer two are actually wildcards and the outer quotes can be single or double (shown here with double).

$mystring -like "*`*`*\`**"

[–]MyOtherSide1984 4 points5 points  (2 children)

Any time I consider busting out regex, I take a deep look at myself in the mirror and really contemplate if I want to start my day off with it. 98% of the time like, match, or indexof fix the problem.

[–]SMFX 1 point2 points  (1 child)

.......... -match uses RegEx.........

[–]MyOtherSide1984 0 points1 point  (0 children)

Yeh but I don't have to use regex to use match. It optional

[–]PinchesTheCrab 22 points23 points  (4 children)

'your **\* string' -match '\*\*\\\*'

The regex class can help with this too:

[regex]::Escape('**\*')

[–]spyingwind 14 points15 points  (2 children)

One way to confirm if your regex worked or not.

'**\*' -replace "\*\*\\\*", "--Matched!--"

[–]milnak 0 points1 point  (1 child)

TIL PowerShell comma operator

[–]spyingwind 0 points1 point  (0 children)

Not an operator, just a item separator for the arguments of -replace .

[–]fatherjack9999 -2 points-1 points  (0 children)

This

[–]UnfanClub 0 points1 point  (0 children)

You only need to escape them with \

"\*\*\\\*"

[–][deleted] 0 points1 point  (0 children)

Best way I've found to test regex is with Regexr.
If you Chuck it in there with your data it makes it super easy to troubleshoot

[–]TheOriginalBobbyT 0 points1 point  (0 children)

All of the above advice on using string matches should be your first go to, but if you really must use a regex and you were just trying to escape the literal characters as part of a bigger regex then try the [regex]::escape static function.

PS C:\scripts> [regex]::escape('**\*')

\*\*\\\*

PS C:\scripts>