split a single string by several seperator
Hi,

I have a string from where i want to get the values for my parameters by splitting the string by the seperator -i, -u, -p.

string:
-ic:\reports\inc\ -ucuriosity -psecret

I want to have those values to be assigned to the variables for further use, like:

$i = c:\reports\inc\
$u = curiosity
$p = secret


the split MUST has to be done using the delimiter -i, -u, -p
  • Edited by TekFinder 15 hours 31 minutes ago
January 13th, 2014 2:49pm

Hi,

Here's a method using Split and SubString:

$str = '-ic:\reports\inc\ -ucuriosity -psecret'
$strArr = $str.Split(' ')

$i = $strArr[0].SubString(2)
$u = $strArr[1].Substring(2)
$p = $strArr[2].Substring(2)

$i
$u
$p

Free Windows Admin Tool Kit Click here and download it now
January 13th, 2014 2:59pm

Hi,

the split MUST has to be done using the delimiter -i, -u, -p. I cannot depend on space (' '), as the order of -i, -u, -p could be changed, like: 

-ucuriosity -psecret -ic:\reports\inc\

So, anywhere in the string it gets -i, it will read the adjacent string until a space or end of string comes. Same for -u and -p



  • Edited by TekFinder 15 hours 22 minutes ago
January 13th, 2014 3:06pm

I'd probably do something like this (though it may need to be tweaked slightly if you have to deal with quoted values that can contain whitespace):

$string = '-ucuriosity -psecret -ic:\reports\inc\'

$u = $i = $p = ''

if ($string -match '(?<=\s|^)-u(\S+)')
{
    $u = $matches[1]
}

if ($string -match '(?<=\s|^)-p(\S+)')
{
    $p = $matches[1]
}

if ($string -match '(?<=\s|^)-i(\S+)')
{
    $i = $matches[1]
}

Write-Host "u: $u"
Write-Host "p: $p"
Write-host "i: $i"

  • Marked as answer by TekFinder 15 hours 12 minutes ago
Free Windows Admin Tool Kit Click here and download it now
January 13th, 2014 3:18pm

Hi David,

This perfectly fulfilled my requirement. Many Thanks. Could you please give me a hint or a link on How the ('(?<=\s|^)-u(\S+)') part is working?

January 13th, 2014 3:30pm

Those are regular expressions.  In english, that would translate to "Immediately following either the beginning of the string or a whitespace character, the characters -u followed by one or more non-whitespace characters."

Breaking it down a bit further:

  • (?<=EXPRESSION):  A zero-width positive lookbehind assertion (I know, it's a mouthful).  Basically, it says that whatever follows must come at a position where the EXPRESSION was matched (but the EXPRESSION itself is not part of the match.)

    Now that I look at what I wrote, that wasn't strictly necessary in this case.  This expression would have worked fine:  '(?:\s|^)-u(\S+)', or even just '(\s|^)-u(\S+)', except in the second case, you'd need to retrieve the value of $matches[2] instead of $matches[1].  More on that later.
  • \s|^ :  A compound regular expression that can match either a single whitespace character (\s), or the position at the beginning of the string (^).  This was to ensure that we didn't match the characters "-u" if they happened to occur in the middle of a word somewhere.  The | character is regex's version of the "-or" operator.
  • -u:  Matches the literal characters "-u"; no special meaning in regex at this point.
  • (\S+):  Matches one or more non-whitespace characters, and saves them in a capturing group.  \S matches any non-whitespace character, and the + symbol is a quantifier that means "one or more".  The parentheses around that part of the expression form a capturing group.  In this case, the group is not named, so they're added to the $matches table by number, in order from left to right.

    $matches[0] always contains the entire match.  $matches[1] will be the value from the first capturing group, and so on.

Free Windows Admin Tool Kit Click here and download it now
January 13th, 2014 3:42pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics