From: Vicente Bosch Date: 2012-09-12T16:55:41+09:00 Subject: Re: String iterate through regex matches with possition --20cf3074b3e049c09104c97c8400 Content-Type: text/plain; charset=UTF-8 Thanks for the answers!! Going to go with Regexp.last_match :-) On 11 September 2012 22:48, 7stud -- wrote: > Vicente Bosch wrote in post #1075485: > > > > Is there any way to scan the string for matches where I get the index > > possition ? > > > > str = 'BS HD SP SP EP SP NL EP EP FT BS' > > str.scan(/ > SP > \s* > (?:NL)* > \s* > EP > /xms) do |match| > md = Regexp.last_match > puts "#{match.inspect} => #{md.offset(0)}" > end > > > --output:-- > "SP EP" => [9, 14] > "SP NL EP" => [15, 23] > > -- > Posted via http://www.ruby-forum.com/. > > --20cf3074b3e049c09104c97c8400 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Thanks for the answers!! Going to go with Regexp.last_match :-)

On 11 September 2012 22:48, 7stud -- <lists@ruby= -forum.com> wrote:
Vicente Bosch wrote in post #1075485:
>
> Is there any way to scan the string for matches where I get the index<= br> > possition ?
>

str =3D 'BS HD SP SP EP SP NL EP EP FT BS'

str.scan(/
=C2=A0 =C2=A0 SP
=C2=A0 =C2=A0 \s*
=C2=A0 =C2=A0 (?:NL)*
=C2=A0 =C2=A0 \s*
=C2=A0 =C2=A0 EP
/xms) do |match|
=C2=A0 md =3D Regexp.last_match
=C2=A0 puts "#{match.inspect} =3D> #{md.offset(0)}"
end


--output:--
"SP EP" =3D> [9, 14]
"SP NL EP" =3D> [15, 23]

--20cf3074b3e049c09104c97c8400--