regex issue

Welcome Forums General PowerShell Q&A regex issue

This topic contains 12 replies, has 4 voices, and was last updated by

 
Participant
3 weeks, 4 days ago.

  • Author
    Posts
  • #179142

    Participant
    Topics: 9
    Replies: 15
    Points: 146
    Rank: Participant

    hi all,

    i have a string like this;




    I wish to use a regex to extract all the PackageID that are in that string. So far, all my tries have returned nothing.

    Could someone please tell me how to do this?

    Thank you!

  • #179154

    Participant
    Topics: 2
    Replies: 497
    Points: 1,230
    Helping Hand
    Rank: Community Hero

    Looks like the forum formatting ate your string. Might have to paste it on a paste site somewhere; the forum tends to gobble up anything that looks like HTML.

  • #179244

    Participant
    Topics: 1
    Replies: 11
    Points: 58
    Rank: Member

    Hi,

    We are not able to see the string. Kindly share the details to help you.

  • #179319

    Participant
    Topics: 9
    Replies: 15
    Points: 146
    Rank: Participant

    sorry, I missed it.

    here's the string;

    MemberProgram PackageID="12345678" ProgramName="Install"/
    MemberProgram PackageID="87654321" ProgramName="Install"/
    MemberProgram PackageID="21436587" ProgramName="Install"/
    MemberProgram PackageID="78563412" ProgramName="Install"/

    each line is between symbols smaller then and greater then, which is why the site removed it.

    thanks!

     

    • #179322

      Participant
      Topics: 1
      Replies: 11
      Points: 58
      Rank: Member

      Hi,

      Are you trying to retrieve the PACKAGE ID from Text file.

  • #179328

    Participant
    Topics: 9
    Replies: 15
    Points: 146
    Rank: Participant

    yes I need the values for packageid

    I tried: $regex = 'PackageID="(.*)"' to capture it but $matches is empty, nothing was capture.

    I'm still new with regex and trying to figure out what I am doing wrong.

    thanks!

    • #179343

      Participant
      Topics: 1
      Replies: 11
      Points: 58
      Rank: Member

      Hi,

      Kindly try the below code.

      $regex = '\d\d\d\d\d\d\d\d'
      $Lines = Get-Content -path 'F:\Scripts\regex\pack.txt' | Where-Object {$_ -match "PackageID"}
      $packID = ($Lines | Select-String -Pattern $regex -AllMatches).Matches.Value
      $packID -match '\d+'
      
      

       

      Note:

      Change the path of the text file

      Thanks & Regards

      Manikandan Krishnasamy

  • #179346

    Participant
    Topics: 1
    Replies: 19
    Points: 52
    Helping Hand
    Rank: Member

    "MemberProgram PackageID=..." -match "PackageID=""(?ThePackageID\d+)"""

    $Matches.ThePackageID

    Note that you'll have to enclose ThePackageID in the regex in smaller than, greater than symbols, I left them out so I could put the line of code directly in the answer.

  • #179352

    Participant
    Topics: 2
    Replies: 497
    Points: 1,230
    Helping Hand
    Rank: Community Hero
    $string -replace '^.+PackageID="(.+)".+$','$1'

    Would be my go-to here, it'll return the desired portion directly. 🙂

  • #179370

    Participant
    Topics: 9
    Replies: 15
    Points: 146
    Rank: Participant
    $string -replace '^.+PackageID="(.+)".+$','$1'
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Would be my go-to here, it'll return the desired portion directly. 🙂

    this gave me something, but more than I need.

    MTL00278" ProgramName="Install
    MTL00639" ProgramName="Install
    MTL00660" ProgramName="Install
    MTL00661" ProgramName="Install

    I need only the MTLxxxxx in that list. Almost there though.

    what is '$1' at the end of the line? I never used that.

    thanks!

  • #179373

    Participant
    Topics: 9
    Replies: 15
    Points: 146
    Rank: Participant

    $string -replace '^.+PackageID="(.+)".+$','$1'

    this gave me something, but more than I need.

    MTL00278" ProgramName="Install
    MTL00639" ProgramName="Install
    MTL00660" ProgramName="Install
    MTL00661" ProgramName="Install

    I need only the MTLxxxxx ID without the rest of the string.

    We're getting closer!

    thanks!

  • #179406

    Participant
    Topics: 2
    Replies: 497
    Points: 1,230
    Helping Hand
    Rank: Community Hero

    Ah, silly me, I should have made the match lazy, or just been more specific. Here:

    $string -replace '^.+PackageID="(.+?)".+$','$1'
    
    # alternate
    $string -replace '^.+PackageID="([^"]+)".+$','$1'
    • #179631

      Participant
      Topics: 9
      Replies: 15
      Points: 146
      Rank: Participant

      that works perfectly.

      now I'll try to understand that regex 😉

      thanks!

You must be logged in to reply to this topic.