[#3228] Core support for Gems, and namespace — "Luke A. Kanies" <luke@...>

Hi all,

21 messages 2004/07/27
[#3230] Re: Core support for Gems, and namespace — Austin Ziegler <halostatue@...> 2004/07/27

On Tue, 27 Jul 2004 11:39:08 +0900, Luke A. Kanies <luke@madstop.com> wrote:

[#3234] Re: Core support for Gems, and namespace — "Luke A. Kanies" <luke@...> 2004/07/27

On Tue, 27 Jul 2004, Austin Ziegler wrote:

[#3238] Re: Core support for Gems, and namespace — Austin Ziegler <halostatue@...> 2004/07/27

On Wed, 28 Jul 2004 00:14:29 +0900, Luke A. Kanies <luke@madstop.com> wrote:

Re: Trying to understand \G

From: "daz" <dooby@...10.karoo.co.uk>
Date: 2004-07-27 15:34:44 UTC
List: ruby-core #3236
From: Matz
>
> \G forces <rest> to match immediately after the last match (or
> beginning of a string).
>
> p "a01b20c30 d40".scan(/[a-z]\d+/)
> p "a01b20c30 d40".scan(/\G[a-z]\d+/)
>
> matz.
>

Ah, "last" meaning "most recent" (contrasted with "final").

At the beginning of a string, \G == \A .
\A can only anchor the beginning of the string once.
\G is the anchor point for the rest (unmatched portion) of the string,
so it can be an anchor point several times in a "repeater" method.

Adding \A to the examples above:

  p 'a01b20c30 d40'.scan(/[a-z]\d+/)    #-> ["a01", "b20", "c30", "d40"]
  p 'a01b20c30 d40'.scan(/\A[a-z]\d+/)  #-> ["a01"]
  p 'a01b20c30 d40'.scan(/\G[a-z]\d+/)  #-> ["a01", "b20", "c30"]

So, \G will be useful for finding sequences which must be contiguous.

Below, both \A and \G versions fail to match and will never match later.

  a3_RE = /[a-z]{3}/
  p ' abcdef'.scan(/#{a3_RE}/)    #=> ["abc ", "def "]
  p ' abcdef'.scan(/\A#{a3_RE}/)  #=> []
  p ' abcdef'.scan(/\G#{a3_RE}/)  #=> []
#----^

Also, just as /.\A/ could never match, nor could /.\G/
Both only make sense at the start of a pattern.

Below, Nobu's example [core:3203] works for \G whereas (substituting
\A for \G in PATT) it confirms that the start of string is at offset
zero and none other.

   words = 'dog horse cat rabbit pig sheep'
   PATT = /\G\w+/

   last = 0
   while index = words.index(PATT, last)
     puts index
     puts $&
     last = Regexp.last_match.end(0) + 1
   end


(Thinking aloud)


:daz




In This Thread