2

I have the following PowerShell script:

$SCRIPTNAME = "myfile.js"
$SUBJECT = Get-Content $SCRIPTNAME | Out-String
if ($SUBJECT -match ".*/// COMMENT.*?$(.*)")
{
    echo $matches[1];
}
$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');

It is generating the following error (I'm trying to use regex capture groups, but its not working):

.* : The term '.*' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\User\Desktop\install.ps1:21 char:39
+ if ($SUBJECT -match ".*/// COMMENT.*?$(.*)")
+                                        ~~
    + CategoryInfo          : ObjectNotFound: (.*:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Why is it having issues with the .* part of the second capture group?

Alexandru
  • 12,264
  • 17
  • 113
  • 208
  • 2
    You have an end of line anchor `$` in front of it which might be causing problems. Either escape it if you want a literal `$` or move it to the end of the pattern. – arco444 Oct 20 '14 at 13:45
  • @arco444 I've tried putting `\$` instead, but this still produces the same error. – Alexandru Oct 20 '14 at 13:48
  • @arco444 Actually, I escaped the inner brackets with `\(` and `\)`...and I think this actually might have worked and/or been the problem. – Alexandru Oct 20 '14 at 13:50
  • 1
    Another issue you might had is that PowerShell would evaluate this as a subexpression `$(.*)` which would also cause an error. Using single quotes on the entire string would fix that. If you are still having an issue showing us some sample data and desired output would help the Community address your issue. – Matt Oct 20 '14 at 14:13
  • @Matt Hey Matt, thanks, I really appreciate the help on this. I am trying to match on the following: http://regex101.com/r/yJ7qU8/1 – Alexandru Oct 20 '14 at 14:24
  • @Matt The problem I am facing now is getting it to be Singleline and Multiline at the same time (how to add flags to it in PowerShell). Not sure how to do that... – Alexandru Oct 20 '14 at 14:33
  • Got it, its the `(?sm)` specifier at the beginning of the regular expression string. Matt, thanks a lot for the tip on single quotes to escape it all, that helped a lot. – Alexandru Oct 20 '14 at 14:44
  • 1
    You need to add (?sm) – Matt Oct 20 '14 at 14:44
  • @Matt How do you check in PowerShell that a regular expression group exists? – Alexandru Oct 20 '14 at 14:48
  • @Matt I guess $matches is an array, so I think `$matches.Cound -ge 2` should do for my case. – Alexandru Oct 20 '14 at 14:59
  • 1
    Use powershell named matched which would be better. [link](http://mjolinor.wordpress.com/2012/03/22/named-captures-in-regular-expressions/) Basically `(?.*)`. Then you will have `$matches.Text` – Matt Oct 20 '14 at 15:00
  • @Matt Perhaps you can help me with another PowerShell dilemma: http://stackoverflow.com/questions/26470423/regex-match-group-to-string-in-powershell – Alexandru Oct 20 '14 at 16:29

1 Answers1

3

Got it. I needed to escape the regular expression with single quotes ' ', and furthermore add single and multiline specifiers (?sm):

$SCRIPTNAME = "myfile.js"
$SUBJECT = Get-Content $SCRIPTNAME | Out-String
if ($SUBJECT -match '(?sm).*/// COMMENT.*?$(.*)' -and $matches.Count -ge 2)
{
    echo $matches[1];
}
$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
Alexandru
  • 12,264
  • 17
  • 113
  • 208