# Mystery regexp

**URL:** <https://www.ruby-forum.com/t/mystery-regexp/98720>\
**Category:** Ruby\
**Created:** [July 17, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/mystery-regexp/98720 "2006-07-17T19:35:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevetooke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@stevetooke](https://www.ruby-forum.com/u/stevetooke)\
**Post date:** [July 17, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/mystery-regexp/98720/1 "2006-07-17T19:35:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![stevetooke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@stevetooke](https://www.ruby-forum.com/u/stevetooke)\
**Post date:** [July 17, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/mystery-regexp/98720/2 "2006-07-17T19:42:10Z")

</div>

Check out [Ruby | zenspider.com | by ryan davis](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.

---

<div class="post-metadata">

**Author:** ![stevetooke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@stevetooke](https://www.ruby-forum.com/u/stevetooke)\
**Post date:** [July 17, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/mystery-regexp/98720/3 "2006-07-17T19:48:42Z")

</div>

On 7/17/06, Steve T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![stevetooke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@stevetooke](https://www.ruby-forum.com/u/stevetooke)\
**Post date:** [July 17, 2006, 7:49pm UTC](https://www.ruby-forum.com/t/mystery-regexp/98720/4 "2006-07-17T19:49:07Z")

</div>

On Jul 17, 2006, at 12:35 PM, Steve T. 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 G. II

---

<div class="post-metadata">

**Author:** ![stevetooke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@stevetooke](https://www.ruby-forum.com/u/stevetooke)\
**Post date:** [July 17, 2006, 7:52pm UTC](https://www.ruby-forum.com/t/mystery-regexp/98720/5 "2006-07-17T19:52:32Z")

</div>

There ya go. Thanks.
