Score:0

RegEx passed by CSV file not working in PowerShell

es flag

Hi I have a script trying to pull out the price value from a html file. the Regex works when I assign it in the script but when I put the regex in CSV, it refuses to give me the result. Could someone help with this?

$htmlcontent = Get-Content ".\Temp.html" -Raw
$priceregex = "(?<=<span class=""a-offscreen"">\$)[\d\.]+"
Write-Host "Regex value is: " $priceregex
IF ($htmlcontent -match $priceregex){$Matches[0]}else{"Not found"}
$csvdata=Import-Csv .\WebMonitor-A.csv
$priceregex=$csvdata[0].Regex
Write-Host "Regex from CSV file is: " $priceregex
IF ($htmlcontent -match $priceregex){$Matches[0]}else{"Not found"}

The html file content looks like this:

<div class="a-section a-spacing-micro">                <span class="a-price aok-align-center" data-a-size="xl" data-a-color="base"><span class="a-offscreen">$10.95</span><span aria-hidden="true"><span class="a-price-symbol">$</span><span class="a-price-whole">10<span class="a-price-decimal">.</span></span><span class="a-price-fraction">95</span></span></span> 

I have this in CSV file as a column (Regex):

(?<=<span class=""a-offscreen"">\$)[\d\.]+
mfinni avatar
cn flag
https://stackoverflow.com/a/1732454/431172
Cliff avatar
es flag
@mfinni Interesting reading. a little bit over-killing. I have never seen an expert gives another solution other than giving you a correct regex, which means the other solution (XML parser?) might not be understood by most of the geeks or it's not good enough in some ways (easy,quick...etc).
Score:0
cn flag

Issue :
When you give the RegEx in the Script , Certain Characters (like Quotes) must be escaped. Hence what you have given is working.

When you give the RegEx in the Textfile Eg CSV , those Characters need not be escaped.
You are still escaping those , hence that will not match.

Solution :
In the CSV text file , give this RegEx :

(?<=<span class="a-offscreen">\$)[\d\.]+

Here , the Quotes (around the Class Name) must not be escaped.

That will work.

Cliff avatar
es flag
Thanks, @Prem. It worked.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.