tags:

views:

33

answers:

3

Hi,

I'm trying to parse a Powershell script for its functions and parameters. I am almost there, byt I am having troubles with newlines. The expression parses test2 to test6 fine but with test1 I cannot get it to work.

I could of course specify my clients do not use the first syntax, but there must be a way. Can someone point me in the right direction? FWIW: I'm using this in C#.

The regex I'm using is: "function (\w*)\s*\{\s*param\((.*)\)\s*}"

content testfile.ps1:

function test1    {
  param([string]$parm1, [int]$parm2,
    [bool]$parm3)}


function test2
{
    param([int]$parm2, [bool]$parm3)

}

function test3
{    param(blabla3)}

function test4 { param(blabla4) }
function test5 {param(blabla5)}
function test6{param(blabla6)}

Thanks, Alex

A: 

Is your regex in single line or multi-line match mode?

I think:

"^function (\w*)\s*\{\s*param\((.*)\)\s*}$"

might work in multi-line mode. I dont have Expresso open available right now to test it.

GrayWizardx
It was multi-line mode. But as mentioned above, the start-of-line and end-of-line options do not work properly.
Alex
Cool. Cool. Glad you got it working.
GrayWizardx
+1  A: 

Because you didn't show you code I'm guessing that you didn't tell the Regex object that newlines were allowed. You can do this with the RegexOptions.Singleline option. This changes the behavior of the . expression to allow it to match against newline (\n). However, if you do your expression could match more than you want. Regular expressions are greedy and this (.*) will consume up to the last parenthesis in the file. You might want to try something like:

function (\w*)\s*\{\s*param\(([^)]*)\)\s*}
James Keesey
This looks good, but note that it doesn’t allow for extra whitespace after `function` or `param`: `^\s*function\s*(\w*)\s*\{\s*param\s*\(([^)]*)\)` I’ve also avoided capturing the rest of the function as it doesn’t look like you need it.
Ciarán Walsh
This indeed does the trick! Thanks! I had experimented with that option but that (.*) probably messed things up.The only thing now is that is seems to create 3 groups instead of 2. There is always a space as the first group. I can eliminate those with a trailing `\s*` but I still do not get why that happens.@ciaran-walsh: the `^` and `$` do not work. Maybe because it's a Singleline now?
Alex
A: 
function (\w*).*?\{.*?[A-Za-z]+\((.*?)\).*?}

Enable multiline matching on "." as mentioned and use this. I tested against http://www.myregextester.com/ and it seemed to work.

I've replaced your \s* with .*?. This matches any character, non-greedy, until the next specified sequence.

Stefan Kendall
Yes, indeed it does the trick! Thanks!
Alex