# Detail on a regex?

**URL:** <https://www.ruby-forum.com/t/detail-on-a-regex/137497>\
**Category:** Ruby\
**Created:** [May 13, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/detail-on-a-regex/137497 "2008-05-13T18:00:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [May 13, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/detail-on-a-regex/137497/1 "2008-05-13T18:00:31Z")

</div>

Is there a way to pull details out of a regular expression’s failure?

I’m running into a situation where I’ve got some validation methods,  
and I’m resorting to passing in both a regular expression and a range.  
I know quite well I could just use a range inside of a regex, and  
would rather, but I need to get the range in order to have some useful  
error messages.

For example, the string must be 3 to 8 characters long, start with a  
letter, and may contain letters numbers or dashes.

I would like to just use this:  
some\_string=~/^[a-z][-a-z0-9]{2,7}$/i

But if the match fails, I can’t give out any good error messages  
without further inspecting the string, so I end up with  
some\_string=~/^[a-z][-a-z0-9]$/i  
[3…8].include?some\_string.length

Just so I can give reasonable error messages.

–Kyle

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [May 13, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/detail-on-a-regex/137497/2 "2008-05-13T18:45:57Z")

</div>

On 13.05.2008 17:59, Kyle S. wrote:

> I would like to just use this:  
> some\_string=~/^[a-z][-a-z0-9]{2,7}$/i
> 
> But if the match fails, I can’t give out any good error messages  
> without further inspecting the string, so I end up with  
> some\_string=~/^[a-z][-a-z0-9]$/i  
> [3…8].include?some\_string.length
> 
> Just so I can give reasonable error messages.

If it does not match you get no information out of the regexp. The  
simplest would be to just include the string in an error message. To  
avoid ugly errors with lengthy strings you could add a length check  
before the regexp match.

In some cases it might be possible to build the regexp so that it  
matches chunks:

some\_string=~/^([a-z])?([-a-z0-9]{2,7})?(.\*)$/i

Now you have a match if $1 and $2 are non nil and $3 is empty.

HTH

robert

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [May 13, 2008, 11:33pm UTC](https://www.ruby-forum.com/t/detail-on-a-regex/137497/3 "2008-05-13T23:33:29Z")

</div>

Hum. Yea, but I want the error messages to be helpful for when I’m  
not the one running the program.  
And also somewhat unique to the reason the error was thrown, just in  
case I need to track it down and find out there’s a logic error on my  
part.

Not that that ever happens…

On Tue, May 13, 2008 at 11:45 AM, Robert K.

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [May 14, 2008, 12:15am UTC](https://www.ruby-forum.com/t/detail-on-a-regex/137497/4 "2008-05-14T00:15:32Z")

</div>

Please do not top post.

On 13.05.2008 23:32, Kyle S. wrote:

> Hum. Yea, but I want the error messages to be helpful for when I’m  
> not the one running the program.

Usually I’d make the error message contain a description of the valid  
format in prose or sufficient examples if I feel that users might not  
know this. An alternative is to dump command line help if there is a  
parameter error.

> And also somewhat unique to the reason the error was thrown, just in  
> case I need to track it down and find out there’s a logic error on my  
> part.

For that you sure have unit tests in place… 🙂

Kind regards

robert
