3

I am writing a powershell script to parse the HTM file. I need to find all the links file in the file and then uppercase the filepath, filename and extention. (could be 30 or 40 links in any file). The part I'm having trouble with is the 2nd part of the -replace staement below (the 'XXXX' part). The regex WILL find the strings I'm looking for but I can't figure out how to 'replace' that string with a uppercase version, then update the existing file with a new links.

I hope I'm explaining this correctly. Appreciate any assistance that anyone can provide.

This is the code I have so far...

$FilePath = 'C:\WebDev'
$FileName = 'Class.htm'
[regex]$regex='(href="[^.]+\.htm")'
#Will Match the string  href="filepath/file.htm"

(  Get-Content "$FilePath\$FileName")  -replace $regex ,  'XXXX' | Set-Content "$FilePath\$FileName";

Final string that gets updated in the existing file should look like this HREF="FILEPATH/FILE.HTM"

Mark Buckley
  • 267
  • 1
  • 4
  • 14

1 Answers1

3

Both beatcracker and briantist refer you to this answer, which shows the correct approach. Regex expressions cannot convert to uppercase, so you need to hook into the .NET String.ToUpper() function.

Instead of using -replace, use the .Replace() method on your $regex object (as described in the other answer). You also need the ForEach-Object construct so it gets called for each string in the pipeline. I've split up the last line for readability, but you can keep it on one line if you must.

$FilePath = 'C:\WebDev'
$FileName = 'Class.htm'
[regex]$regex='(href="[^.]+\.htm")'

(Get-Content "$FilePath\$FileName") |
   ForEach-Object { $regex.Replace($_, {$args[0].Value.ToUpper()}) } |
   Set-Content "$FilePath\$FileName"
Community
  • 1
  • 1
JamesQMurphy
  • 4,214
  • 1
  • 36
  • 41
  • Hi JamesQMurphy, this is a good answer, but [if you agree that it's a duplicate, you should not post an answer](http://meta.stackexchange.com/q/10841/268785). My comment was added automatically because I cast a close vote; you probably aren't able to see the close votes yet so you didn't know, but essentially if 4 more people vote the same (or any one gold tag badge holder) it will be closed and the duplicate will be linked for everyone. So based on that I recommend deleting the answer (and again, it's a good one, I don't mean to take away from your effort). – briantist Jun 03 '16 at 01:40
  • @briantist I disagree that this question is a duplicate of that one. The other question asks about using Lambda expressions in PowerShell. In fact, the other question states that the author "is not about solving this regular-expression problem." This question asks about how to convert to uppercase with regular expressions. The Lambda expression is one part of the answer; the `ForEach-Object` part is the other. – JamesQMurphy Jun 03 '16 at 01:46
  • Ah I understand; but as said in the linked meta post: `Questions may be duplicates if they have the same (potential) answers.` This doesn't necessarily mean the question will be the same, but that the answer can be found there. Additionally, the "regex problem" referenced by that asker was referring to writing the regex itself; the question was primarily the same one. However you have a point about the iteration aspect of it, so I've retracted my close vote. – briantist Jun 03 '16 at 01:58
  • @briantist Thanks... I've also marked this as community wiki. – JamesQMurphy Jun 03 '16 at 02:00
  • Well it doesn't hurt anything but I don't think this is a good case for community wiki personally, and you deserve the rep for the answer, so if you can change it back I'd so go ahead. – briantist Jun 03 '16 at 02:01