Help with regex

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

I have a mailbox folder structure in this format:

/CustomerXYZ/A/A.1
/CustomerXYZ/A/A.24
/CustomerXYZ/A/A.3
/CustomerXYZ/B/B.60776
/CustomerXYZ/B/B.93555
/CustomerXYZ/B/B.33
/CustomerXYZ/C/C.1alpha
/CustomerXYZ/C/C.2Bravo
/CustomerXYZ/C/C.3tango

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:

/CustomerXYZ/A/A.1

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 "/".

/CustomerXYZ/A/A.1
/CustomerXYZ/A.1/folder1
/CustomerXYZ/A^1/Folder2
/CustomerXYZ/B/B.60776
/CustomerXYZ/B-1/B.93555
/CustomerXYZ/B#2/B.33
/CustomerXYZ/C_1/C.1alpha
/CustomerXYZ/C/C.2Bravo
/CustomerXYZ/C/C.3tango
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:

'^/CustomerXYZ/[^/]+$'
Here’s that regex in use with a collection based on the paths that you provided:

$folders = @(
'/CustomerXYZ/A'
'/CustomerXYZ/A/A.1'
'/CustomerXYZ/A.1'
'/CustomerXYZ/A.1/folder1'
'/CustomerXYZ/A^1'
'/CustomerXYZ/A^1/Folder2'
'/CustomerXYZ/B'
'/CustomerXYZ/B/B.60776'
'/CustomerXYZ/B-1'
'/CustomerXYZ/B-1/B.93555'
'/CustomerXYZ/B#2'
'/CustomerXYZ/B#2/B.33'
'/CustomerXYZ/C_1'
'/CustomerXYZ/C_1/C.1alpha'
'/CustomerXYZ/C'
'/CustomerXYZ/C/C.2Bravo'
'/CustomerXYZ/C/C.3tango'
)
$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