mystery regexp

Discussion in 'Ruby' started by Steve Throckmorton, Jul 17, 2006.

  1. I found this regexp in the docs for the OptionParser class, and I don't
    understand it. Google hasn't helped, nor has the Pickaxe, so I thought
    I would ask here. The purpose of the code is to ensure that a file
    extension submitted by the user begins with a period (a dot).

    file_ext.sub!(/\A\.(?=.)/, ".")

    I understand all except (?=.) As far as I can tell the regexp works
    fine without it. I can delete the dot inside the parens, and the code
    still runs. I can change the = to a - and the code runs, but if I
    substitute a letter or a * instead, it doesn't.

    Someone please clue me in here. What is this thing?
    Steve Throckmorton, Jul 17, 2006
    #1
    1. Advertising

  2. Steve Throckmorton

    gregarican Guest

    Check out http://www.zenspider.com/Languages/Ruby/QuickRef.html#11 for
    details on this portion of the regex. It seems to involve look-ahead
    capability.

    HTH.

    Steve Throckmorton wrote:
    > I found this regexp in the docs for the OptionParser class, and I don't
    > understand it. Google hasn't helped, nor has the Pickaxe, so I thought
    > I would ask here. The purpose of the code is to ensure that a file
    > extension submitted by the user begins with a period (a dot).
    >
    > file_ext.sub!(/\A\.(?=.)/, ".")
    >
    > I understand all except (?=.) As far as I can tell the regexp works
    > fine without it. I can delete the dot inside the parens, and the code
    > still runs. I can change the = to a - and the code runs, but if I
    > substitute a letter or a * instead, it doesn't.
    >
    > Someone please clue me in here. What is this thing?
    gregarican, Jul 17, 2006
    #2
    1. Advertising

  3. There ya go. Thanks.


    gregarican wrote:

    > Check out http://www.zenspider.com/Languages/Ruby/QuickRef.html#11 for
    > details on this portion of the regex. It seems to involve look-ahead
    > capability.
    >
    > HTH.
    >
    > Steve Throckmorton wrote:
    >
    >>I found this regexp in the docs for the OptionParser class, and I don't
    >>understand it. Google hasn't helped, nor has the Pickaxe, so I thought
    >>I would ask here. The purpose of the code is to ensure that a file
    >>extension submitted by the user begins with a period (a dot).
    >>
    >>file_ext.sub!(/\A\.(?=.)/, ".")
    >>
    >>I understand all except (?=.) As far as I can tell the regexp works
    >>fine without it. I can delete the dot inside the parens, and the code
    >>still runs. I can change the = to a - and the code runs, but if I
    >>substitute a letter or a * instead, it doesn't.
    >>
    >>Someone please clue me in here. What is this thing?

    >
    >
    Steve Throckmorton, Jul 17, 2006
    #3
  4. Steve Throckmorton

    Jason Sweat Guest

    On 7/17/06, Steve Throckmorton <> wrote:
    > I found this regexp in the docs for the OptionParser class, and I don't
    > understand it. Google hasn't helped, nor has the Pickaxe, so I thought
    > I would ask here. The purpose of the code is to ensure that a file
    > extension submitted by the user begins with a period (a dot).
    >
    > file_ext.sub!(/\A\.(?=.)/, ".")
    >
    > I understand all except (?=.) As far as I can tell the regexp works
    > fine without it. I can delete the dot inside the parens, and the code
    > still runs. I can change the = to a - and the code runs, but if I
    > substitute a letter or a * instead, it doesn't.
    >
    > Someone please clue me in here. What is this thing?
    >


    \A start of subject (independent of multiline mode)

    (?= is a positive look ahead assetion

    so \A the begining of the input \. is a literal . (and there must be
    something more than that afterwords.

    $ irb
    >> re = /\A\.(?=.)/

    => /\A\.(?=.)/
    >> re.match('abc')

    => nil
    >> re.match('.')

    => nil
    >> re.match('.a')

    => #<MatchData:0x40988d7c>
    >> re.match('.123')

    => #<MatchData:0x409879a4>
    >> re.match('..')

    => #<MatchData:0x409866a8>


    --
    Regards,
    Jason
    http://blog.casey-sweat.us/
    Jason Sweat, Jul 17, 2006
    #4
  5. On Jul 17, 2006, at 12:35 PM, Steve Throckmorton wrote:

    > file_ext.sub!(/\A\.(?=.)/, ".")
    >
    > I understand all except (?=.)


    That's a look-ahead assertion, ensuring there is at least one
    character following the period. Observe:

    >> test = ".hidden_file"

    => ".hidden_file"
    >> test.sub!(/\A\.(?=.)/, ".")

    => ".hidden_file"
    >> test = "."

    => "."
    >> test.sub!(/\A\.(?=.)/, ".")

    => nil

    Hope that helps.

    James Edward Gray II
    James Edward Gray II, Jul 17, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John K. Humkey

    sed regexp mystery

    John K. Humkey, Jul 8, 2003, in forum: Perl
    Replies:
    0
    Views:
    2,035
    John K. Humkey
    Jul 8, 2003
  2. Greg Hurrell
    Replies:
    4
    Views:
    149
    James Edward Gray II
    Feb 14, 2007
  3. Mikel Lindsaar
    Replies:
    0
    Views:
    463
    Mikel Lindsaar
    Mar 31, 2008
  4. Joao Silva
    Replies:
    16
    Views:
    340
    7stud --
    Aug 21, 2009
  5. Uldis  Bojars
    Replies:
    2
    Views:
    185
    Janwillem Borleffs
    Dec 17, 2006
Loading...

Share This Page