Jump to content

How to truncate text in a variable


Recommended Posts

This would work if my string will always be "OU=Corporate", but it could be "OU=Chicago" or several other locations, im think I need to split the strings like you have done but into a case where I can always select the 3rd case....

You were given the answer. Split the string using a comma then the 4th element of that array is the text after the third comma

$string = "CN=Last\, First,OU=Accounts,OU=Corporate,OU=FSGN,DC=fsgn,DC=net"

$array = StringSplit($string, ",")

$outputWanted = $array[4]
Link to comment
Share on other sites

To allow for the possibility they might someday embed a comma in the portion of the string you're looking at, you might want to do some character replacements before doing your StringSplit, and process your input string something like this?

#include <Array.au3> 

$string = "CN=Last\, First,OU=Accounts,OU=Corporate,OU=FSGN,DC=fsgn,DC=net"

$temp = StringReplace($string, "\,", chr(255))
$temp = StringReplace($temp, ",", chr(254))
$temp = StringReplace($temp, chr(255), ",")
$array = StringSplit($temp, chr(254))
_ArrayDisplay($array)


$outputWanted = $array[3]
MsgBox(1,"",$outputWanted)

I could have nested all those puppies into a single line, but it wouldn't have made it very readable. I stuck the _arraydisplay in just to show what it did to the first parameter (the one that already has the embedded comma).

Link to comment
Share on other sites

You were given the answer. Split the string using a comma then the 4th element of that array is the text after the third comma

$string = "CN=Last\, First,OU=Accounts,OU=Corporate,OU=FSGN,DC=fsgn,DC=net"

$array = StringSplit($string, ",")

$outputWanted = $array[4]
Sorry was just replying to posts, thank you much =)
Link to comment
Share on other sites

I'd think you would almost have to do the character substitutions I suggested above. Your company has defined the field you're after as the third in the string. Using comma delimiters and treating it as a static fourth field is flawed. Were a value in the first field that did not contain the "/," to come along, or values in the second or third field that did contain a "/,", your target data would either no longer be the fourth in the array, or only a portion of it would be found there. Doing the character substitutions, all the fields in the string will be parsed correctly ("/," converted to "," and not treated as a delimiter) and the field you're after will always be returned in the third element of the array.

Edited by Spiff59
Link to comment
Share on other sites

This StringRegExpReplace() should work, without all the character substitution schemes and regardless of lengths of other strings

$string = "CN=Last\, First,OU=Accounts,OU=Corporate,OU=FSGN,DC=fsgn,DC=net"

$outputWanted = StringRegExpReplace($string,"(?i).*OU=.*(OU=.*),.*OU=.*","\1")

MsgBox(1,"",$outputWanted)
Link to comment
Share on other sites

There is something wrong with your Regex simply by what if you don't have the one before it and after it OC=. You fail to seek out the OC=Corporate then. See I told you how to accomplish this task by several means but people just don't seem to get it. $sValue = "CN=Last\, First,OU=Accounts,OU=Corporate,OU=FSGN,DC=fsgn,DC=net" but can be anything within reason

StringRegExp($sValue, "(?i)((\w*=)|(\, ))(\w*)", 4)oÝ÷ Ú­²Ø^µ«$vÞÂ+aǦ§±§(Ø­ÚÚwbµ8^*.«ÞÊx¬çi®nrاÇF®¶­se7G&ætå7G"b33c·5fÇVRÂgV÷C´õSÔ6÷'÷&FRgV÷C²oÝ÷ ÚØb³¥©pk+0¢¹yÆ®±ì¨º·âاìZ­«[ºÒ0¢é]i×bµ©è¶«¶§«­¢+Ù%MÑÉ¥¹%¹MÑÈ ÀÌØíÍY±Õ°ÅÕ½Ðí=Tõ
½ÉÁ½ÉÑÅÕ½ÐìðÅÕ½ÐìÀäÈì°
½ÉÁ½ÉÑÅÕ½Ðì¤

This will accomplish the task too by you definition of whats going on

0x576520616C6C206469652C206C697665206C69666520617320696620796F75207765726520696E20746865206C617374207365636F6E642E

Link to comment
Share on other sites

If his requirements are that he's given a comma-delimited string, with the caveat that commas within the string fields themselves have had a "\" inserted behind them and should not be treated as delimiters, and he's told to test the thrid string for a certain value, then none of those 4 examples will work in all cases.

EDIT: I spose I'm thinking more in corporate terms, and of the type of standard parsing code one would want to put into a production environment. A lot of the methods here will work only for the particular string he presented. If a value like "OC=Accounts\, Collections" were found in the second field, or they run across a person who legally has only one name, at that point all the StringInStr or StringRegExp methods fall apart. The StringSplit method suggested by others, with some preprocessing to handle embedded commas ("\,"), seems the best way to ensure that when you test the third field, that you are actually looking at the correct, and complete, third field. It also gives you a nice complete array with a parm count and each parameter formatted and ready for any processing.

Edited by Spiff59
Link to comment
Share on other sites

If his requirements are that he's given a comma-delimited string, with the caveat that commas within the string fields themselves have had a "\" inserted behind them and should not be treated as delimiters, and he's told to test the thrid string for a certain value, then none of those 4 examples will work in all cases.

EDIT: I spose I'm thinking more in corporate terms, and of the type of standard parsing code one would want to put into a production environment. A lot of the methods here will work only for the particular string he presented. If a value like "OC=Accounts\, Collections" were found in the second field, or they run across a person who legally has only one name, at that point all the StringInStr or StringRegExp methods fall apart. The StringSplit method suggested by others, with some preprocessing to handle embedded commas ("\,"), seems the best way to ensure that when you test the third field, that you are actually looking at the correct, and complete, third field. It also gives you a nice complete array with a parm count and each parameter formatted and ready for any processing.

I overlooked the "\," non-delimeter sequence :)

But I believe you'll find this RegExp does what the OP was asking, regardless of persons who legally have only one name or where "\," might show up later as in "OC=Accounts\, Collections" or any other permutations

$string = "CN=Last\, First,OU=Accounts,OU=Corporate,OU=FSGN,DC=fsgn,DC=net"

$aOutputWanted = StringRegExp($string, "(?U)(?<!\\),.*(?<!\\),(.*)(?<!\\),",1)
MsgBox(1,"",$string & @CRLF & @CRLF & $aOutputWanted[0])

$string = "CN=Only one legal name,OU=Accounts,OU=Corporate,OU=FSGN,DC=fsgn,DC=net"

$aOutputWanted = StringRegExp($string, "(?U)(?<!\\),.*(?<!\\),(.*)(?<!\\),",1)
MsgBox(1,"",$string & @CRLF & @CRLF & $aOutputWanted[0])


$string = "CN=last\,First\,Middle,OU=Accounts\,Collections,OU=Corporate,OU=FSGN,DC=fsgn,DC=net"

$aOutputWanted = StringRegExp($string, "(?U)(?<!\\),.*(?<!\\),(.*)(?<!\\),",1)
MsgBox(1,"",$string & @CRLF & @CRLF & $aOutputWanted[0])

Just to prove a point :lmao:

But I do agree with you on your method if you want all the data parsed neatly for future reference.

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