Sign in to follow this  
Followers 0
kor

StringRegExp help

6 posts in this topic

I have a piece of data

CN=308-142413,OU=Workstations,OU=Administrative,OU=Admin-Sites,DC=TEST,DC=COMPANY,DC=ORG

I would like to delete everything before the first OU statement (ou=workstations) effectively getting rid of 

CN=308-142413, (including that pesky comma)

But I'm not sure how to accomplish this deletion without accidentally deleting the rest of the string (if I based things on commas, or OU)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

$str = "CN=308-142413,OU=Workstations,OU=Administrative,OU=Admin-Sites,DC=TEST,DC=COMPANY,DC=ORG"

$res = StringRegExpReplace($str, '^.*?,' , "")
Msgbox(0,"", $res)

Means : delete anything .*? between the beginning ^ and the comma , (comma included)

Edit

Don't forget the classic way

$str = "CN=308-142413,OU=Workstations,OU=Administrative,OU=Admin-Sites,DC=TEST,DC=COMPANY,DC=ORG"

$res = StringReplace($str, StringSplit($str, ",")[1] & ",", "")
Msgbox(0,"", $res)

:)

Edit2

If "everything before the first OU" may include a comma, it could be done like this

$str = "CN=308-142413,something,OUelse,OU=Workstations,OU=Administrative,OU=Admin-Sites,DC=TEST,DC=COMPANY,DC=ORG"

$res = StringRegExpReplace($str, '^.*?(OU=.*)' , "$1")
Msgbox(0,"", $res)
Edited by mikell

Share this post


Link to post
Share on other sites

Thank you so much.

Share this post


Link to post
Share on other sites

mikell's last pattern is the correct one if your predicate string can contain more than one comma as in ...

local $str = 'CN=308-142413,fouteen,OU=Workstations,OU=Administrative,OU=Admin-Sites,DC=TEST,DC=COMPANY,DC=ORG'

$res = StringRegExpReplace($str, '^.*?(OU=.*)' , "$1")
Msgbox(0,"", $res)

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

@kylomas : the comma should me escaped in this case :

local $str = 'CN=308-142413\,fouteen,OU=Workstations,OU=Administrative,OU=Admin-Sites,DC=TEST,DC=COMPANY,DC=ORG'

http://social.technet.microsoft.com/wiki/contents/articles/5312.active-directory-characters-to-escape.aspx

Share this post


Link to post
Share on other sites

@jguinch - Yes, but I am not going to make assumptions/force requirements on user input.


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0