tags:

views:

55

answers:

1

Hi I'm trying to extract the parameters from a class definition.

class locale.company.app.LoginData(username:String, password:String)

I have extracted the package name and class name, but the parameters are causing me some trouble.

I used Rubular to construct the following regex:

http://www.rubular.com/regexes/9597

According to Rubular this should work but it doesn't, as it will only match the first parameter. So i tried this regex instead:

classDescription = "class locale.company.app.LoginData(username:String, password:String)"
fieldMatches = classDescription.match(/(((\w+\s*):(\s*\w+))(\s*,\s*)?)+/)

I print out the matches using the following code:

fieldMatches.to_a.each {|m| puts m}

And I get this result:

username:String, password:String
password:String
password:String
password
String
,

This is only matching the second parameter for some reason. Does anyone know method to extract the parameters?

A: 

I think you want to use "scan" rather than "match":

classDescription = "class locale.company.app.LoginData(username:String, password:String)"
fieldMatches = classDescription.scan(/(\w+\s*):(\s*\w+)/)
fieldMatches.each {|m| puts m}

which gives:

username
String
password
String
Martin Owen
Excellent thanks Martin, I just knew there had to be a greedy method. Thanks again
Brian Heylin