Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - regex help

Reply
 
Old 10-13-2008   #1 (permalink)
Jason


 
 

regex help

I'll be the first to say that I don't know jack about regex, and I need to.
That aside, I am asking for assistance with a simple problem.

When I use get-qaduser and check the MemberOf member, I would like to
shorten its output to just show the group name only.

For instance, here's my current, standard output:
[PS] C:\>$b.MemberOf -match "G-"
CN=g-MKT Sales Operations Book,OU=g - global
groups,OU=Sales,OU=Marketing,OU=Security Group Admin,OU=Information Protec
tion,OU=ABC - Corporate Applications,DC=abc,DC=corp,DC=company,DC=com
CN=g-ABCFIL22 Users,OU=g - global groups,OU=Marketing,OU=Security Group
Admin,OU=Information Protection,OU=ABC - Cor
porate Applications,DC=abc,DC=corp,DC=company,DC=com

And I'd like to have it output this instead:
CN=g-MKT Sales Operations Book
CN=g-ABCFIL22 Users

Thus, I only need the text up until the first comma in the text.

Any help is appreciated. Also, and good easy-to-understand regex learning
resource would be helpful!

-Jason

My System SpecsSystem Spec
Old 10-13-2008   #2 (permalink)
Kiron


 
 

RE: regex help

Hi Jason,
I can't test any AD code but can help with the RegEx. Use the -Replace
operator to capture and replace all chars up to the first comma:

# mind word wrap
$strArray = 'CN=g-MKT Sales Operations Book,OU=g - global
groups,OU=Sales,OU=Marketing,OU=Security Group Admin,OU=Information
Protection,OU=ABC - Corporate Applications,DC=abc,DC=corp,DC=company,DC=com',
'CN=g-ABCFIL22 Users,OU=g - global groups,OU=Marketing,OU=Security Group
Admin,OU=Information Protection,OU=ABC - Corporate
Applications,DC=abc,DC=corp,DC=company,DC=com'
$strArray -replace '([^,]+).+','$1'

# here's another way
$strArray | % {$_.split(',')[0]}


I'd recommend this site, it's a great tutorial on RegEx:
http://www.regular-expressions.info/tutorialcnt.html
--
Kiron
My System SpecsSystem Spec
Old 10-13-2008   #3 (permalink)
RickB


 
 

Re: regex help

On Oct 13, 10:29*am, Jason <Ja...@xxxxxx> wrote:
Quote:

> I'll be the first to say that I don't know jack about regex, and I need to. *
> That aside, I am asking for assistance with a simple problem.
>
> When I use get-qaduser and check the MemberOf member, I would like to
> shorten its output to just show the group name only. *
>
> For instance, here's my current, standard output:
> [PS] C:\>$b.MemberOf -match "G-"
> CN=g-MKT Sales Operations Book,OU=g - global
> groups,OU=Sales,OU=Marketing,OU=Security Group Admin,OU=Information Protec
> tion,OU=ABC - Corporate Applications,DC=abc,DC=corp,DC=company,DC=com
> CN=g-ABCFIL22 Users,OU=g - global groups,OU=Marketing,OU=SecurityGroup
> Admin,OU=Information Protection,OU=ABC - Cor
> porate Applications,DC=abc,DC=corp,DC=company,DC=com
>
> And I'd like to have it output this instead:
> CN=g-MKT Sales Operations Book
> CN=g-ABCFIL22 Users
>
> Thus, I only need the text up until the first comma in the text.
>
> Any help is appreciated. *Also, and good easy-to-understand regex learning
> resource would be helpful!
>
> -Jason
This matches text up to the first comma.
'$[^,]+'

But your case doens't eve need to mess with that.
You can split on comma and select the first group by it's index.

$a = 'text containing, comma seperated, text'

($a.split(','))[0]

Or simply select the substring up to the comma

$a.substring(0,$a.indexof(','))
My System SpecsSystem Spec
Old 10-21-2008   #4 (permalink)
Jacob Saaby Nielsen


 
 

Re: regex help

Hello Jason,

if you want a really good RegEx tool, I would strongly recommend buying RegexBuddy.
It's worth every penny. And
a lot better than the freeware ones you can find (at least I think so, and
I gained much from it, still do, when I had
to learn Regular Expressions).

Anyway...

That aside, I think there's a much simpler solution to your problem, than
dabbling with regexes.

"CN=($b.MemberOf -match "G-").name"

Simply put, create a string that starts with CN=, and then expand the name
of the group and put if after CN=.

Of course, you need to do a for each loop to do it for each group you find,
but that's pretty simple too.

Just thought it was a bit more pragmatic than regexes, and wanted to present
another solution to your problem

(Bear in mind I haven't tested the specific code, am in a bit of a hurry
here, but the principle should work just fine).

--
Kind Regards,
Jacob Saaby Nielsen
http://www.ijacob.info

PowerShell trouble ? http://www.powershellcommunity.org
Quote:

> I'll be the first to say that I don't know jack about regex, and I
> need to. That aside, I am asking for assistance with a simple
> problem.
>
> When I use get-qaduser and check the MemberOf member, I would like to
> shorten its output to just show the group name only.
>
> For instance, here's my current, standard output:
> [PS] C:\>$b.MemberOf -match "G-"
> CN=g-MKT Sales Operations Book,OU=g - global
> groups,OU=Sales,OU=Marketing,OU=Security Group Admin,OU=Information
> Protec
> tion,OU=ABC - Corporate Applications,DC=abc,DC=corp,DC=company,DC=com
> CN=g-ABCFIL22 Users,OU=g - global groups,OU=Marketing,OU=Security
> Group
> Admin,OU=Information Protection,OU=ABC - Cor
> porate Applications,DC=abc,DC=corp,DC=company,DC=com
> And I'd like to have it output this instead:
> CN=g-MKT Sales Operations Book
> CN=g-ABCFIL22 Users
> Thus, I only need the text up until the first comma in the text.
>
> Any help is appreciated. Also, and good easy-to-understand regex
> learning resource would be helpful!
>
> -Jason
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Regex help please? PowerShell
regex help PowerShell
Regex Help PowerShell
Regex Help .NET General
regex PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46