From: Scott Gonyea Date: 2010-11-11T11:22:22+09:00 Subject: Re: Total string length regex My guess is, you're wanting to check the total length of what they entered... As the length of their username must be (for example) between 5 and 20 characters long? If so, I'd say you're making things needlessly complex. min = 5 max = 20 # ... user_name = params[:user_name] return false unless( (min..max).include?(user_name.length) # check size and user_name =~ /^\w/ # must start with word-character and user_name =~ /\w$/ # must end with word-character and user_name !~ /\W\W/) # no two non-word-characters together User.create(user_name) On Nov 10, 2010, at 6:04 PM, Shea Barton wrote: > So I have a complicated regex, and I need to have a condition that > checks the total number of characters in a string, on top of the other > conditions. > > (this is for a username, to make sure they don't put more than one > special character in a row) > > /^\w(?:\w*(?:[-\.]\w+)?)*$/ > > How would I limit the total string length for this regex to be between x > and y characters? > > thanks > > -- > Posted via http://www.ruby-forum.com/. >