Perl \G in Ruby regex?

In Perl, \G matches the end position of the last match.

In Ruby, it matches the start position of the last match.

\G in Perl is very usual when you need to break down a complex regex
and iterate through it with different regex.

Is there anyway I can have the Perl \G behavior in Ruby?

Chris

Hi,

In Perl, \G matches the end position of the last match.

In Ruby, it matches the start position of the last match.

\G in Perl is very usual when you need to break down a complex regex
and iterate through it with different regex.

Is there anyway I can have the Perl \G behavior in Ruby?

\G works in String#gsub(!), #scan and #index.

p “-abc-def–ghi-”.gsub(/\G-\w+/){$&.upcase}
#=> “-ABC-DEF–ghi-”
p “-abc-def–ghi-”.scan(/\G-\w+/)
#=> [“-abc”, “-def”]
p “-abc-def-ghi-”.index(/\G\w+/) #=> nil
p “-abc-def-ghi-”.index(/\G\w+/, 1) #=> 1
p “-abc-def-ghi-”.index(/\G\w+/, 5) #=> 5

···

At Wed, 29 Jan 2003 05:39:43 +0900, Chris Wong wrote:


Nobu Nakada