Help for regex

Welcome Forums General PowerShell Q&A Help for regex

Viewing 4 reply threads
  • Author
    Posts
    • #199145
      Participant
      Topics: 12
      Replies: 36
      Points: 32
      Rank: Member

      Hello people,
      Once again I count on the help of the masters! I’m making a query on a device via SNMP and it returns me a data similar to the one below:

      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      

      I need to remove the quotes and get only the first information, in this case name01. Obviously the data collected is diverse and the number of characters in each field is also variable. That is, in one query it can be name01 and in another it can be nonononono01.

      I believe it is via regex, but I have no idea how to do it.

      I thank you for your support.

    • #199148
      Participant
      Topics: 12
      Replies: 1489
      Points: 1,987
      Helping Hand
      Rank: Community Hero

      You can use ConvertFrom-CSV to remove the qoutes and a calculated expression to split the string where there is an underscore ( _ ). This turns the string into an array:

      PS C:\Users\rasim> $temp[0].Name -split '_'
      name01
      data01
      data02
      data03
      

      Then we just reference the index of the first item, which is represented by 0:

      $temp = @"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name0103013013030_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01_data01_data02_data03"
      "name01010101010_data01_data02_data03"
      "@ | ConvertFrom-Csv -Header Name
      
      $temp | Select Name, @{Name='ParsedName';Expression={($_.Name -split '_')[0]}}
      

      Output:

      Name                                   ParsedName       
      ----                                   ----------       
      name01_data01_data02_data03            name01           
      name01_data01_data02_data03            name01           
      name01_data01_data02_data03            name01           
      name01_data01_data02_data03            name01           
      name01_data01_data02_data03            name01           
      name01_data01_data02_data03            name01           
      name0103013013030_data01_data02_data03 name0103013013030
      name01_data01_data02_data03            name01           
      name01_data01_data02_data03            name01           
      name01_data01_data02_data03            name01           
      name01_data01_data02_data03            name01           
      name01_data01_data02_data03            name01           
      name01010101010_data01_data02_data03   name01010101010  
      
    • #199208
      Participant
      Topics: 12
      Replies: 36
      Points: 32
      Rank: Member

      Hi Rob, thank you very much for your reply,
      These data are already in an array and I managed via regex to remove most of the string I need, the situation is now like this:

      "name01
      "name02
      "name03
      "name04"
      "name05"
      "name06
      "name07
      "name08
      "name09
      "name10
      "name11
      "name12
      "name13
      

      Note that some lines have quotation marks at the beginning and another at the end, while others only at the beginning.

      Once again, thank you very much for your support.

    • #199226
      Participant
      Topics: 4
      Replies: 70
      Points: 192
      Helping Hand
      Rank: Participant

      If all you need to do is remove quotes, you can use the -replace operator (or replace string method) and replace every instance of ” with an empty string.  For example:

      '"name' -replace '"',''
    • #199229
      Participant
      Topics: 12
      Replies: 36
      Points: 32
      Rank: Member

      Hi Mr Mike,

      Its works!!!

       

      Thank you so much!!!

Viewing 4 reply threads
  • You must be logged in to reply to this topic.