Frage

What I'd like to do is create a function that is passed a string to match a regular expression inside of the function. Let's call the function "matching." It should use the -match command. I want it to meet the criteria:

The < character
Four alphabetic characters of upper of lowercase (a-z or A-Z)
The > character
The - character
Four digits, 0-9

So basically it would just look like "matching whateverstringisenteredhere" then it'd give me true or false. Probably incredibly simple for you guys, but to someone new at powershell it seems really difficult.

So far I have this:

function matching ($args0)
{
$r = '\b[A-Za-z]{4}[0-9]{4}<>-\b'
$r -match ($args0)
}

The problem seems to be it's not treating it as a regular expression inside the function. It's only taking it literally.

War es hilfreich?

Lösung

The regex goes on the right side of the -match operator and the string to be matched goes on the left. Try:

$args0 -match $r
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top