Re: any 3 letters

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
regular expressions:

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

If you really want a single regexp, then you can explicitly code a match
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
newlines, so that they can be more easily understood.



This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs