0

I have this regex capture group:

$lowerPattern='(href[\s]?=[\s]?\"[^"]*[^"]*\")'

which is returning all the matches I need just fine. However I need to replace the capture group with the results all lowercase:

$lowerPatternReplace = '$1'.ToLower()

This doesn't seem to be working. How you lowercase a capture group in powershell regex?

John
  • 31
  • 6
  • The repeated pattern `[^"]*` in your RE doesn't make sense to me, the 1st one will greedily get all none `"` chars and the 2nd will be empty. –  Apr 01 '19 at 18:26

1 Answers1

1

This code seems to work for me. It's just a bit less shorthand. I didn't see a way to do it with backreferences, due to the order of execution (you're lowering the literal string '$1').

$Entry = 'asdHREFasd'
$RegEx = '(href)'
$match = $Entry -match $RegEx
[string]$upper = $Matches[1] #first capture group
[string]$lower = $upper.ToLower()
[string]$Entry.replace($upper,$lower)

source

Jacob Colvin
  • 2,625
  • 1
  • 17
  • 36
  • `$Entry = 'asdHREFasd'; $RegEx = '(href)'; if($Entry -match $RegEx){$Entry = $Entry.Replace($Matches[1],$Matches[1].ToLower())}` –  Apr 01 '19 at 18:25
  • Fixed it with this. Thanks. Looks like the $Matches was the key. – John Apr 01 '19 at 18:56