Help with regex

by maryk at 2012-09-10 15:01:09

I have a mailbox folder structure in this format:


I am trying to get a folder statistics but only up to the second level. meaning, I only need to use get-mailboxfolderstatisctics to get me the folder /CustomerXYZ/A or /CustomerXYZ/B or /CustomerXYZ/c but not the next level Here is the command I am using:

Get-MailboxFolderStatistics "mailbox" | where {$_.folderpath -match ‘/CustomerXYZ/[a-zA-Z0-9]/’} | select folderpath

This gives me all folder and not up to the second "/" which I am looking for. Any ideas?
by DonJ at 2012-09-10 15:23:08
Let’s see.

Consider starting with ^ to anchor the match to the start.

But the problem is that a your regex is satisfied by the whole thing. For example:


The boldfaced part satisfies the regex. The rest is just along for the ride. Is just /CustomerXYZ/A/[\b] returned in your folder output? If so, add a $ to the end of your regex, and it’ll anchor the end of the string. Anything going beyond that won’t be a match anymore.
by maryk at 2012-09-10 17:16:14
I have Ttried your suggestion but it’s not giving me any output. Forgot to mention that the name of the folders can also have some special characters. Below is an example that shows that acceptable character can exist after CustomerXYZ/ abd before the second "/".

by poshoholic at 2012-09-10 18:01:40
You should probably just use a regex that checks for any character that is not /, followed by the /, like this:

Here’s that regex in use with a collection based on the paths that you provided:

$folders = @(
$folders -match '^/CustomerXYZ/[^/]+$'

The result is a collection of paths with a depth of 2.
by willsteele at 2012-09-10 18:04:53
I often use this trick for regular directories. Not sure if it applies here:

Get-ChildItem dir//

That will only go two deep, but, get everything. Again, this is for regular folder exploration. Wildcards may get you there without needing full-blown regexes.
by maryk at 2012-09-11 16:41:03
That worked. Thanks