Need to use regular expressions inside of a function to match a string passed to it

StackOverflow https://stackoverflow.com/questions/23449930

  •  14-07-2023
  •  | 
  •  

Question

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.

Était-ce utile?

La solution

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top