regex issue

hi all,

i have a string like this;

<MemberProgram PackageID=“12345678” ProgramName=“Install”/>
<MemberProgram PackageID=“87654321” ProgramName=“Install”/>
<MemberProgram PackageID=“21436587” ProgramName=“Install”/>
<MemberProgram PackageID=“78563412” ProgramName=“Install”/>

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!

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.

Hi,

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

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!

 

Hi,

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

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!

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

“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.

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

Would be my go-to here, it’ll return the desired portion directly. :slight_smile:

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!

$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!

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'

that works perfectly.

now I’ll try to understand that regex :wink:

thanks!