Jump to content

StringRegExp help


Go to solution Solved by mikell,

Recommended Posts

Posted

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)

  • Solution
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
Posted

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

Posted

@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

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
  • Recently Browsing   0 members

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