# Re: any 3 letters

**URL:** <https://www.ruby-forum.com/t/re-any-3-letters/142096>\
**Category:** Ruby\
**Created:** [July 3, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/re-any-3-letters/142096 "2008-07-03T12:30:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [July 3, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/re-any-3-letters/142096/1 "2008-07-03T12:30:39Z")

</div>

> xxx765xxx should match  
> vvv123vvv should be omitted
> 
> is it possible to write proper regular expression?

Yes, it’s possible, but you may find it less mind-bending just to use  
two  
regular expressions:

puts “match” if foo =~ /\A[a-z]{3}\d{3}[a-z]{3}\z/ && foo !~  
/\A…123/

If you really want a single regexp, then you can explicitly code a match  
for  
the allowed digit ranges:

re = %r{\A [a-z]{3}  
([02-9]\d\d | 1[013-9]\d | 12[0124-9])  
[a-z]{3} \z}x

Or you can be clever and use a negative look-ahead assertion:

re = %r{\A [a-z]{3} (?!123) \d{3} [a-z]{3} \z}x

Note that %r{…}x is just a way to write regexps with embedded spaces  
and  
newlines, so that they can be more easily understood.

HTH,

Brian.
