I need some help with a regular expression for a validates_format_of
statement in my model. I have a user login field and i only want to
allow the login field to have alphanumeric characters and the underscore
( a-z, A-Z, 1-9, _ ) those are the only characters I want to allow.
What is the properly Ruby RegExp to do this that I would use in the
:with => // statement of the validates_format_of
The \w character class is all alphanumerics plus underscore -- and the
\W character class is the opposite. Assuming you really don't want to
allow zero, you could do:
:with => /[^\W0]+/
i.e., no character (that's the ^) that is either \W or 0.
Note, however, that there's been some flux in the question of whether
or not your regex gets automatically wrapped by beginning and
end-of-string anchors. That regex assumes that the anchors are added
(though I hope in the long run they aren't). Try some tests, and if
you need to, you can wrap it in anchors like this:
> ^ and $ match beginning and end of line, \A and \Z match beginning and
> end of string. You want \A and \Z.
I'd go for \z, because \Z discounts a final newline:
Thanks for the info. I must have missed the memo about Ruby regexes
being different from Perl. Are there other differences and Is this
documented anywhere?
^ and $ match beginning and end of line, \A and \Z match beginning and
end of string. You want \A and \Z.
I'd go for \z, because \Z discounts a final newline:
Thanks for the info. I must have missed the memo about Ruby regexes
being different from Perl. Are there other differences and Is this
documented anywhere?
I think the memo would have been if they were exactly the same as
Perl's The anchors should be documented in most or all extended
discussions of Ruby regexes (though they may or may not mention how
these compare to Perl). I've seen the second edition of the Friedl
book but don't own it, and I don't remember how detailed it gets in
its Ruby comparisons.
One area to focus on in the Perl/Ruby comparison is the modifiers.
Since Ruby has anchors for both line and string, it doesn't need the
/m modifier as it's defined in Perl. Ruby's /m modifier is like
Perl's /s: it adds newline to the . character class.
FYI, Perl has \A, \Z, and \z, too. In Perl, the meaning of ^ and $ change with the use of the /m modifier and that's why it's common to see /ms or /xms on Perl regexps. With Ruby, I'd expect to see /m or /xm on most complex patterns.
I was surprised as how hard it was to find the modifiers in Ruby listed in the Pickaxe, but they're in chapter 22 ("The Ruby Language") starting on page 324.
The other significant way that the Perl and Ruby (1.8) regexps differ is in the semantics of executing code during the match. Perl allow code in the replacement text with the /e modifier on a substitution where Ruby just passes the match off to a block.