Jump to content

StringRegExp help


kor
 Share

Go to solution Solved by mikell,

Recommended Posts

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)

Link to comment
Share on other sites

  • Solution

$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
Link to comment
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

Link to comment
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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...