[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

mystery regexp

Steve Throckmorton

7/17/2006 5:32:00 PM

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?
4 Answers

greg.kujawa

7/17/2006 5:37:00 PM

0

Check out http://www.zenspider.com/Languages/Ruby/QuickR... 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

7/17/2006 5:46:00 PM

0

There ya go. Thanks.


gregarican wrote:

> Check out http://www.zenspider.com/Languages/Ruby/QuickR... 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?
>
>

Jason Sweat

7/17/2006 5:47:00 PM

0

On 7/17/06, Steve Throckmorton <nntp.m.ttwelve@xoxy.net> 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...

James Gray

7/17/2006 5:48:00 PM

0

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