# More informative return value for Regexp

**URL:** https://rubytalk.org/t/more-informative-return-value-for-regexp/18356
**Category:** ruby-talk
**Created:** [12 May 2005 19:06 UTC](https://rubytalk.org/t/more-informative-return-value-for-regexp/18356 "2005-05-12T19:06:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)
#### Post date: [12 May 2005 19:06 UTC](https://rubytalk.org/t/more-informative-return-value-for-regexp/18356/1 "2005-05-12T19:06:40Z")

</div>

Is there a way to get a Regexp to tell you if it didn't match because  
it won't match with the current input, or if it doesn't match (yet)  
but potentially could with more input? Failing that, is there an  
alternative regexp library for ruby that will allow that? I ask  
becasue I'd like to do something along the lines of:

File.open(some\_file) do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.scan(/a regex/) do |res|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something with res  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
end

I realise I could just load the whole file into memory, but that seems  
kind of well, icky.

---

<div class="post-metadata">

### Author: ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)
#### Post date: [12 May 2005 19:43 UTC](https://rubytalk.org/t/more-informative-return-value-for-regexp/18356/2 "2005-05-12T19:43:55Z")

</div>

There is a discussion around something related to this on RCRchive.  
The upshot of the discussion is that it is not possible with regexen  
in general, and lies way outside of the bounds of regexen.

-austin

> **···**
>
> On 5/12/05, Logan Capaldo \<logancapaldo@gmail.com\> wrote:
> 
> > Is there a way to get a Regexp to tell you if it didn't match because  
> > it won't match with the current input, or if it doesn't match (yet)  
> > but potentially could with more input? Failing that, is there an  
> > alternative regexp library for ruby that will allow that? I ask  
> > becasue I'd like to do something along the lines of:
> > 
> > File.open(some\_file) do |f|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.scan(/a regex/) do |res|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something with res  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > I realise I could just load the whole file into memory, but that seems  
> > kind of well, icky.
> 
> --  
> Austin Ziegler \* halostatue@gmail.com  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Alternate: austin@halostatue.ca

---

<div class="post-metadata">

### Author: ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)
#### Post date: [12 May 2005 20:33 UTC](https://rubytalk.org/t/more-informative-return-value-for-regexp/18356/3 "2005-05-12T20:33:30Z")

</div>

Add your voice to RCR179  
&nbsp;&nbsp;[RCR 179: Create a 'NoMatchData' object on Regex match failing.](http://www.rcrchive.net/rcr/show/179)

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
New Zealand

Somewhere on the edge of a Galaxy, one of literally billions of such  
galaxies, is a sun, one of literally billions of suns in that  
galaxy.

> **···**
>
> On Fri, 13 May 2005, Logan Capaldo wrote:
> 
> > Is there a way to get a Regexp to tell you if it didn't match because  
> > it won't match with the current input, or if it doesn't match (yet)  
> > but potentially could with more input? Failing that, is there an  
> > alternative regexp library for ruby that will allow that? I ask  
> > becasue I'd like to do something along the lines of:

---

<div class="post-metadata">

### Author: ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)
#### Post date: [12 May 2005 20:43 UTC](https://rubytalk.org/t/more-informative-return-value-for-regexp/18356/4 "2005-05-12T20:43:50Z")

</div>

I've wanted this functionality in the past. I always thought of it  
in the context of accepting interactive input. As someone was typing  
you could provide input as to whether the partial string would be  
rejected, might be accepted, or was already valid. Simple example  
would be a 5-digit zip code field. As long as the user was typing  
digits the UI could indicate "ok so far". If they typed a non-digit  
the UI could indicate "no good" and at the point they had typed  
exactly five digits the UI could indicate "acceptable as is". If  
the user continued typing beyond the five digits, the UI would indicate  
"no good".

Gary Wright

> **···**
>
> On May 12, 2005, at 3:43 PM, Austin Ziegler wrote:
> 
> > > Is there a way to get a Regexp to tell you if it didn't match because  
> > > it won't match with the current input, or if it doesn't match (yet)  
> > > but potentially could with more input?

---

<div class="post-metadata">

### Author: ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)
#### Post date: [13 May 2005 06:45 UTC](https://rubytalk.org/t/more-informative-return-value-for-regexp/18356/5 "2005-05-13T06:45:23Z")

</div>

Aha. So much for that idea.

> **···**
>
> On 5/12/05, Austin Ziegler \<halostatue@gmail.com\> wrote:
> 
> > On 5/12/05, Logan Capaldo \<logancapaldo@gmail.com\> wrote:  
> > \> Is there a way to get a Regexp to tell you if it didn't match because  
> > \> it won't match with the current input, or if it doesn't match (yet)  
> > \> but potentially could with more input? Failing that, is there an  
> > \> alternative regexp library for ruby that will allow that? I ask  
> > \> becasue I'd like to do something along the lines of:  
> > \>  
> > \> File.open(some\_file) do |f|  
> > \> f.scan(/a regex/) do |res|  
> > \> # do something with res  
> > \> end  
> > \> end  
> > \>  
> > \> I realise I could just load the whole file into memory, but that seems  
> > \> kind of well, icky.
> > 
> > There is a discussion around something related to this on RCRchive.  
> > The upshot of the discussion is that it is not possible with regexen  
> > in general, and lies way outside of the bounds of regexen.
> > 
> > -austin  
> > --  
> > Austin Ziegler \* halostatue@gmail.com  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Alternate: austin@halostatue.ca
