string manipulation

This topic contains 7 replies, has 5 voices, and was last updated by  Emilio Mari 3 years, 6 months ago.

  • Author
    Posts
  • #14475

    Barney Stinson
    Participant

    Location :
    Name : slave-pos (redirected 2)
    PrinterState : 0
    PrinterStatus : 3
    ShareName :
    SystemName : NWTL-XCHANGE1

    Hi I have this text output. I want to use powershell to get only "slave-pos(redirected 2)" How can I do this in Powershell. I am new to ti.

    Operating System: Windows 2008 r2 x64

  • #14476

    Dave Wyatt
    Moderator

    Is that actually a string, or is that list output of a PowerShell object? If it's an object, you'd just do $object.Name . Assuming that this is text in a single string variable (not an array), then this should work:

    $text = @'
    Location :
    Name : slave-pos (redirected 2)
    PrinterState : 0
    PrinterStatus : 3
    ShareName :
    SystemName : NWTL-XCHANGE1
    '@
    
    $name = ''
    
    if ($text -match '(?m)^\s*Name\s*:\s*(.+?)\s*$')
    {
        $name = $matches[1]
    }
    
    $name
    
  • #14498

    Tore Groneng
    Participant

    hi,

    If you love to hate regex 🙂 (sorry Dave), this also works:


    $name = $text -split [environment]::NewLine | foreach { if($_.startswith("Name")){$_.split(":")[1]} }
    $name

    or


    $name = $text -split "`n" | foreach { if($_.startswith("Name")){$_.split(":")[1]} }
    $name

  • #14500

    Dave Wyatt
    Moderator

    I can't help it. I learned regular expressions at a formative age, and now they're like my coding security blanket. 😛

  • #14933

    Emilio Mari
    Participant

    Dave can you explain in plain text the regex above?

    • #14936

      Dave Wyatt
      Moderator

      No problem:

      (?m)^\s*Name\s*:\s*(.+?)\s*$

      • (?m) : This sets the pattern to "multiline mode", which means that the ^ an $ anchors will match the beginning and ending of each line, respectively, instead of only the beginning and ending of the entire string.
      • ^ : This matches the beginning of any line (in multiline mode.)
      • \s*Name\s*:\s* : This matches zero or more whitespace characters followed by the word Name, followed again by zero or more whitespace characters, a colon, and zero or more whitespace characters again. This would match "Name:", " Name : ", etc, so long as no other non-whitespace was found before Name at the beginning of the line.
      • (.+?) : The parentheses are a capturing group. Whatever text is eventually matched by the regex pattern inside them can be accessed later, in this case by checking $matches[1]. The ".+" part matches one or more characters, and the "?" makes the + operator lazy (meaning it will match as few characters as possible.) I made the operator lazy because I didn't want the "." wildcard to match...
      • \s*$ : Any trailing whitespace before the end of the line.
  • #14934

    Vern Anderson
    Participant
  • #14940

    Emilio Mari
    Participant

    Thanks Dave. Very nice class about Regex. Soon as I wrote my post, I found this page

    http://positivedb.wordpress.com/2014/02/06/

    and I could follow your class much better.

    Thanks a lot.
    I also want to thanks Vern Anderson for his redirection.

You must be logged in to reply to this topic.