Rodrigo Alves Vieira
2011-Feb-11 19:30 UTC
Regex for string first chars - Model validation
Hello everyone, in my Rails app I need to validate a string that on creation can not have its first chars empty or composed by any special chars. For example: " file" and "%file" aren''t valid. Do you know what Regex I should use? Thanks! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Fri, Feb 11, 2011 at 11:30 AM, Rodrigo Alves Vieira <rodrigo3n-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> Hello everyone, in my Rails app I need to validate a string that on > creation can not have its first chars empty or composed by any special > chars. > > For example: " file" and "%file" aren''t valid. Do you know what Regex I > should use? >You could utilize the "anchor" for this -- that is you say that the line must "begin" with the characters that match the following regex. Something like: /\A[\w]+$/ would work.> Thanks! > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip Hallstrom
2011-Feb-11 19:47 UTC
Re: Regex for string first chars - Model validation
> On Fri, Feb 11, 2011 at 11:30 AM, Rodrigo Alves Vieira <rodrigo3n-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > Hello everyone, in my Rails app I need to validate a string that on creation can not have its first chars empty or composed by any special chars. > > For example: " file" and "%file" aren''t valid. Do you know what Regex I should use? > > You could utilize the "anchor" for this -- that is you say that the line must "begin" with the characters that match the following regex. > Something like: /\A[\w]+$/ would work.Note that \w will match an underscore. Don''t know if you consider that "special" or not... -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rodrigo Alves Vieira
2011-Feb-11 19:48 UTC
Re: Regex for string first chars - Model validation
Hello, thanks for the reply, but after applying this regex I go to rails console and run a = File.new a.file_name = " example.txt" It still works, when it shouldn''t... any more help? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rodrigo Alves Vieira
2011-Feb-11 19:57 UTC
Re: Regex for string first chars - Model validation
Undescore is allowed. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rodrigo Alves Vieira
2011-Feb-11 19:58 UTC
Re: Regex for string first chars - Model validation
Underscore is allowed. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip''s idea seems pretty good: ^\w+ That says: look for any string that starts with one or more "word" characters (letters, numbers, underscores). Try it out in rubular: http://rubular.com/ It let''s you quickly experiment with lots of regular expressions and has a quick-ref right there on the page. On Feb 11, 11:58 am, Rodrigo Alves Vieira <rodrig...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Underscore is allowed.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.