Total string length regex

S

Shea Barton

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
 
S

Scott Gonyea

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 =3D 5
max =3D 20

# ...
user_name =3D params[:user_name]

return false unless(=20
(min..max).include?(user_name.length) # check size
and user_name =3D~ /^\w/ # must start =
with word-character
and user_name =3D~ /\w$/ # must end =
with word-character
and user_name !~ /\W\W/) # no two =
non-word-characters together

User.create(user_name)
 
S

Shea Barton

I know I could do it withs several lines of ruby, but I'd really like to
have it contained all within a single regex, as I am using it with
validates_format_of :with => /.../ in rails.
 
S

Scott Gonyea

Pretty sure you can repeat "validates_format_of" as much as you like.

Regular expressions should always be broken up into simpler, easy to =
understand, chunks. Especially when you're validating user names and =
the like.

If you do smash it all together, you write tests for all of your edge =
cases and point that at a constant, containing your regex, at the top of =
your model. To strengthen your regex muscles:

http://rubular.com/

Scott
 
S

Shea Barton

I've actually been using rubular - but unfortunately it isn't quite as
helpful for more advanced regexes involving lookaheads / behinds etc.

Although I found a proper regex that worked using Joel's example, a
better solution to the problem was just to use my existing regex with
validates_format_of, in conjunction with validates_size_of
 
A

Ammar Ali

I've actually been using rubular - but unfortunately it isn't quite as
helpful for more advanced regexes involving lookaheads / behinds etc.

Although I found a proper regex that worked using Joel's example, a
better solution to the problem was just to use my existing regex with
validates_format_of, in conjunction with validates_size_of

It's worth noting that \w and \W do not handle utf-8 properly in all
cases, even with the u option. If you plan to allow/support i18n'ed
usernames, consider using Unicode character properties in you regular
expressions. For more information take a look at:

http://ruby.runpaint.org/regexps#properties

These properties are only available in ruby 1.9 though.

Regards,
Ammar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

RegEx 0
Minimum Total Difficulty 0
Regex replace problem 2
Help for my project in the last minute 0
Converting an Array to a String in JavaScript 7
Need Assistance With A Coding Problem 0
Tasks 1
Regex help 6

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top