locked
Help with RegEX RRS feed

  • Question

  • Hello All,

    Newbie Alert!!!

    I have a text in below format where want to capture fields like id,idversion,os,guid from the text.

    id=useraccount;idversion=2.0;os=windows;guid=235876dd-1as5-8mc5-5j9j-1b79048e70a2

    If I use '^id=([^;]+).+$','$1' as regex, it works fine for first item but if I change it to '^idversion=([^;]+).+$','$1', this returns whole text back.

    e.g. $text = 'id=useraccount;idversion=2.0;os=windows;guid=235876dd-1as5-8mc5-5j9j-1b79048e70a2'

    $text -replace '^id=([^;]+).+$','$1' 

    returns useraccount

    but

    $text -replace '^idversion=([^;]+).+$','$1' 

    returns whole $text value.

    Basically I want to capture everything after id= or idversion= or os= or guid= until first semicolon or end of string like guid doesn't have any semicolon at the end.

    Thanks in advance.




    • Edited by PSPR Tuesday, July 3, 2018 3:13 PM
    Tuesday, July 3, 2018 2:21 PM

Answers

  • Here's one way you could accomplish this:


    $inputString = 'id=useraccount;idversion=2.0;os=windows;guid=235876dd-1as5-8mc5-5j9j-1b79048e70a2'
    
    $inputString | Select-String '([^=;]+)=([^;]+)' -AllMatches | ForEach-Object {
      $outputObject = New-Object PSObject
      $_.Matches | ForEach-Object {
        $outputObject | Add-Member NoteProperty $_.Groups[1].Value $_.Groups[2].Value
      }
      $outputObject
    }
    

    This example outputs a custom object with 4 properties: id, idversion, os, and guid.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 3, 2018 4:27 PM

All replies

  • Here's one way you could accomplish this:


    $inputString = 'id=useraccount;idversion=2.0;os=windows;guid=235876dd-1as5-8mc5-5j9j-1b79048e70a2'
    
    $inputString | Select-String '([^=;]+)=([^;]+)' -AllMatches | ForEach-Object {
      $outputObject = New-Object PSObject
      $_.Matches | ForEach-Object {
        $outputObject | Add-Member NoteProperty $_.Groups[1].Value $_.Groups[2].Value
      }
      $outputObject
    }
    

    This example outputs a custom object with 4 properties: id, idversion, os, and guid.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 3, 2018 4:27 PM
  • Thanks Bill for reply.

    I don't want a custom object to be returned. The output will go directly into some non-windows systems.

    So I am looking for a regex that can just give me value after id= or idversion or os= or guid= until first semicolon or end of string.

    Tuesday, July 3, 2018 5:08 PM
  • That's not a problem.

    You can specify the properties from the custom object. If the above is in a script (e.g., C:\Scripts\MyScript.ps1), you could, for example, write:


    PS C:\> $customObject = C:\Scripts\MyScript.ps1
    PS C:\> $customObject.id
    useraccount
    PS C:\> $customObject.os
    windows
    

    ...and so forth. This will probably make a bit more sense once you learn a more about how PowerShell works.


    -- Bill Stewart [Bill_Stewart]


    Tuesday, July 3, 2018 5:18 PM
  • PS D:\scripts> $text -match 'idversion=(\d+\.\d+);'
    True
    PS D:\scripts> $matches[1]
    2.0

    Or by name:

    PS D:\scripts> $text -match 'idversion=(?<idversion>\d+\.\d+);'
    True
    PS D:\scripts> $matches['idversion']
    2.0
    PS D:\scripts>

    You can do multiple captures in one match by naming the captures.


    \_(ツ)_/


    • Edited by jrv Tuesday, July 3, 2018 5:39 PM
    Tuesday, July 3, 2018 5:37 PM