# Matchdata

**URL:** <https://rubytalk.org/t/matchdata/33155>\
**Category:** ruby-talk\
**Created:** [23 November 2006 11:12 UTC](https://rubytalk.org/t/matchdata/33155 "2006-11-23T11:12:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremy2](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Jeremy2](https://rubytalk.org/u/Jeremy2)\
**Post date:** [23 November 2006 11:12 UTC](https://rubytalk.org/t/matchdata/33155/1 "2006-11-23T11:12:48Z")

</div>

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()  
&nbsp;&nbsp;&nbsp;&nbsp;op1()  
&nbsp;&nbsp;&nbsp;&nbsp;op2()  
end sub

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

public function method3() as string  
&nbsp;&nbsp;&nbsp;&nbsp;op1()  
&nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard2](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Simon\_Strandgaard2](https://rubytalk.org/u/Simon_Strandgaard2)\
**Post date:** [23 November 2006 11:19 UTC](https://rubytalk.org/t/matchdata/33155/2 "2006-11-23T11:19:25Z")

</div>

[snip]

> What I want:  
> [MatchData,MatchData,MatchData]

something like this

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

> **···**
>
> On 11/23/06, Jeremy Wells \<jwells@servalsystems.co.uk\> wrote:
> 
> --  
> Simon Strandgaard

---

<div class="post-metadata">

**Author:** ![Jeremy2](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Jeremy2](https://rubytalk.org/u/Jeremy2)\
**Post date:** [23 November 2006 16:33 UTC](https://rubytalk.org/t/matchdata/33155/3 "2006-11-23T16:33:34Z")

</div>

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.co.uk) \<[http://www.servalsystems.co.uk](http://www.servalsystems.co.uk)\>  
> Tel: 01342 331940  
> Fax: 01342 331950
