kardock
September 23, 2019, 1:20pm
1
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!
ta11ow
September 23, 2019, 2:49pm
2
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.
kardock
September 24, 2019, 8:01am
4
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.
kardock
September 24, 2019, 8:17am
6
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
krisvg
September 24, 2019, 8:36am
8
“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.
ta11ow
September 24, 2019, 8:43am
9
$string -replace '^.+PackageID="(.+)".+$','$1'
Would be my go-to here, it’ll return the desired portion directly.
kardock
September 24, 2019, 9:09am
10
$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!
kardock
September 24, 2019, 9:14am
11
$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!
ta11ow
September 24, 2019, 10:44am
12
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'
kardock
September 25, 2019, 8:21am
13
that works perfectly.
now I’ll try to understand that regex
thanks!