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 !

4

1 回答 1

2

这应该适合你:

搜索

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

替换(使用变量值)

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

或(带变量名)

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

示例: http ://regexr.com?357f4

编辑

对于循环,您可以尝试 3 个正则表达式。首先会简单地改变你的开始:

搜索

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

代替

Log("Method $1:", 

示例:http ://regexr.com?357fp

然后你会做“循环”,虽然不是真的循环。

搜索

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

代替

"$1:", $2

示例:http ://regexr.com?357g2

然后你删除结局

搜索

/\).*/gi

代替

)

示例:http ://regexr.com?357g5

于 2013-06-13T15:56:53.247 回答