Count number of strings within a string?

Using regex:

$a = "blah test <= goes here / blah test <= goes here / blah blah"
[regex]$regex = '<= goes here /'
$regex.matches($a).count
2

I had a string with a bunch of pipes in it. I wanted to know how many there were, so I used this to get it. Just another way :)

$ExampleVar = "one|two|three|four|fivefive|six|seven";
$Occurrences = $ExampleVar.Split("|").GetUpperBound(0);
Write-Output "I've found $Occurrences pipe(s) in your string, sir!";
  • Marcus

another way (similar to @mjolinor way) in one line:

([regex]::Matches($a, "<= goes here /" )).count