3

I want to transform this:

Public [Function|Sub] XXXX(ByVal param1 As aaaa, ByVal param2 AS bbbb) As cccc

Into this:

Log("Method XXXX:", "param1", param1, "param2", param2)

The number of parameters is variable.

Can I do it in pure regexp, and if so, how can I do it ?
I will use a simple tool like this: http://gskinner.com/RegExr/ to do it manually for each method.

I am here:

Public (Function|Sub) ([\w\d_]+)\((ByVal .* As .*)*\)( As [\w]+)?
Log("Method $2:", $3)

Which gives me this:

Log("Method XXXX:", ByVal param1 As aaaa, ByVal param2 AS bbbb)

It's a small step forward, but not really a big one...

The problem being, I don't know if (and how) it's possible to catch a repeating sub-item. Other questions point to it not being possible ?

I need to do it in pure regexp, not in code. Otherwise, I will use copypasta, but I would love to maximize the automation.

Thanks !

thomasb
  • 5,816
  • 10
  • 57
  • 92
  • http://stackoverflow.com/questions/5018487/regular-expression-with-variable-number-of-groups – ctn Jun 13 '13 at 15:49
  • 1
    is the number of param defined? And what language will you use? (cause there is different regex flavour) – Casimir et Hippolyte Jun 13 '13 at 16:07
  • I put more precisions in my question: the number of parameters is variable, and I use a tool (like online, or Notepad++ or such). I don't want to write code for this. – thomasb Jun 13 '13 at 16:49

1 Answers1

2

This should work for you:

Search

/^[^\]]*[\]] ([\w\d_]+)\(ByVal ([^ ]*) As ([^,]*), ByVal ([^ ]*) As ([^,]*)\)( As [\w]+)/gi

Replace (with variable value)

Log("Method $2:", $3, "$4", $5)

OR (with variable name)

Log("Method $2:", $2, "$4", $4)

Example: http://regexr.com?357f4

EDIT

For looping, you could try 3 regular expressions. First would simply change your beginning:

Search

/^[^\]]*[\]] ([\w\d_]+)/gi

Replace

Log("Method $1:", 

Example: http://regexr.com?357fp

Then you would do the "looping", though not really looping.

Search

/\({0,1}ByVal ([^ ]*) As([^,\)]*[\)]{0,1})/gi

Replace

"$1:", $2

Example: http://regexr.com?357g2

Then you remove the ending

Search

/\).*/gi

Replace

)

Example: http://regexr.com?357g5

David Starkey
  • 1,840
  • 3
  • 32
  • 48