[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

matchdata

Jeremy Wells

11/23/2006 11:13:00 AM

I have a file that I want to scan for matches of a regular expression.
The expression crosses multiple lines (/m), so I can't use each_line.
I've been using scan, but I now need a match data object for each match
so I can get the position of the match. Is there a way to loop through
matches and get the match data for each match when all the data is in a
single string with multiple matches in it?

For arguments sake, here is the data:

public sub method1()
op1()
op2()
end sub

public sub method2(lots, of,
variables, over, many, lines)
op1()
op2()
end sub

public function method3() as string
op1()
op2()
end function

Here is the regular expression:
/^(?:(private|public)\s)?(sub|function)\s(.+?)\s?\(.*?\)(?:\sas\s(.+?))?(?:'.*?)?$.*?end\s\2/im

What is current returned by scan:
['public','sub','method1',nil]
['public','sub','method2',nil]
['public','function','method3','string']

What I want:
[MatchData,MatchData,MatchData]

Jeremy

2 Answers

Simon Strandgaard

11/23/2006 11:19:00 AM

0

On 11/23/06, Jeremy Wells <jwells@servalsystems.co.uk> wrote:
[snip]
> What I want:
> [MatchData,MatchData,MatchData]


something like this

'aa'.scan(/a/) {|i| p $~ }

--
Simon Strandgaard

Jeremy Wells

11/23/2006 4:34:00 PM

0

thanks, that worked great

Simon Strandgaard wrote:
> On 11/23/06, Jeremy Wells <jwells@servalsystems.co.uk> wrote:
> [snip]
>> What I want:
>> [MatchData,MatchData,MatchData]
>
>
> something like this
>
> 'aa'.scan(/a/) {|i| p $~ }
>
> --
>
> Simon Strandgaard
>

--


*Jeremy Wells*
Serval Systems Ltd.

www.servalsystems.co.uk <http://www.servalsystems...
Tel: 01342 331940
Fax: 01342 331950