1

I have a problem with a quick function I wrote for splitting Sharepoint specific ids ({id};#{name}):

function GetOnlyValue {
param( 
[string] $fullValue
)
if(![string]::IsNullOrEmpty($fullValue))
{
 # On regarde si on a la valeur
 if($fullValue -Like "*;#*")
 {
 $array = $fullValue -split ";#", 2, "simplematch"
 return $array[-1]
 }
}
return $fullValue
}

But this function does half the job because, for instance, when I pass a value formated like that : myId;#myName the function return me this value: "#myName" instead of "myName".

Thanks for your help !

06/02/2016 EDIT: This function is included in a module that I import in a script. I use it in correlation with a Sharepoint 2013 site, browsing each SPListItem of a SPList:

$formation = GetOnlyValue -fullValue $spItem["Formation"]
asked Jun 2, 2016 at 14:57
4
  • Cannot reproduce here. 'myId;#myName' -split ';#',2,'simplematch' works as expected. Commented Jun 2, 2016 at 15:01
  • I used simple and double quotes, I've always the same result :/ Commented Jun 2, 2016 at 15:16
  • try to output $spItem["Formation"] and ensure its a string Commented Jun 2, 2016 at 15:48
  • Hi, so it works now, by ensuring the data is a string -__- : GetOnlyValue -fullValue ([string]$item["Formation"]) Thanks to all for your help ! Commented Jun 3, 2016 at 7:22

2 Answers 2

1

I'd use a regex with capture groups:

function GetOnlyValue
{
 Param
 ( 
 [string]$fullValue
 )
 if($fullValue -match '(.*);#(.*)'){
 $Matches[2]
 }
}
answered Jun 2, 2016 at 15:04

3 Comments

Not really a reason for that; their code should work fine. Not everything needs to be bashed at with regexes, especially if the code is not broken to begin with.
@Joey Yep, so it's up to OP to decide. I'm merely suggesting.
Nope, even with the regex, I have the problem
0

I can't reproduce your problem, but you can simplify your script using a -replace:

function GetOnlyValue {
 param( 
 [string] $fullValue
 )
 $fullValue -replace '^.*#'
}
answered Jun 2, 2016 at 15:23

5 Comments

Again, it cut at the ';' but not at the ';#' group.Thanks anyway !
For instance: 457;#FirstName LastName - Organization. I have in output: #FirstName LastName - Organization
I have in output FirstName LastName - Organization.
Note that your code is not identical to what they have in their question. For a string containing two # yours would remove everything up to the second, whilst theirs would split at the first.
yes, to fix that he had to make the dot non greedy (`^.*?#')

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.