Railsters: ActiveRecord''s validation system puts other database systems to shame. However, the newbies might not know how to write a regexp that excludes a match, instead of tests for it. Understand - I''m just asking this question to help them. I have been using Regexps since ''grep'' on Xenix! But the newbies here might not know how to do this: validates_format_of :field_must_break, :with => /\n/ validates_format_of :field_must_not_break, :with => /^[^\n]$/ The second one fails with data that correctly have no \n linefeed in them. How do I - I mean "they" - negate a regexp? -- Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!! --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
validates_format_of :field_must_not_break, :with => /[^\n]/ There you - I mean "they" go -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Keynan Pratt wrote:> validates_format_of :field_must_not_break, :with => /[^\n]/That passes on the first non-\n character. /^[^\n]+$/ doesn''t work either. (Trying for "all characters between start and end not \n".) New question: Why don''t any variations of Regexp have a bulk negation operator? Most applications can rely on !~. When we can''t use that, or when we must negate a subset of a regexp, the result is super-ugly. The contrapositive of /\n/ should be just as clean-looking, right? -- Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!! --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
so you wanted this then? validates_format_of :field_must_not_break, :with => /^[^\n]*$/ -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Phlip wrote:> New question: Why don''t any variations of Regexp have a bulk negation > operator? Most applications can rely on !~. > > When we can''t use that, or when we must negate a subset of a regexp, > the result is super-ugly. The contrapositive of /\n/ should be just as > clean-looking, right? >dude thats not a regexp problem. its simple if(not str.includes?("\n")) done -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
which I belive you can do with validates_exclusion_of -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Keynan Pratt wrote:> so you wanted this then? > validates_format_of :field_must_not_break, :with => /^[^\n]*$/assert_match /^[^\n]+$/, "kozmik\nbullfrog" That assertion passes - we want it to fail. Next, there are two questions afloat - how to validate (without resorting to validate(), on principle), and whether all regexp libraries everywhere have a big gap. You can''t pass a validating block to validates_exclusion_of; only a proc to determine if the validation should take place. It gets passed the model, so we could put the ''not str.includes?'' in there, but that''s much more code than simply using a validate(). And ''not str.includes?'' doesn''t answer the philosophical question about regexps! -- Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!! --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Are you looking for a field with only one character [which can''t be a newline]? ''Cause that''s what /^[^\n]$/ does. You might want to add some wildcardier metacharacters to include more possible characters than just one. Or you might just want to ask this question over and over. Excuse me, "they" might want to. ;) RSL On 2/16/07, Phlip <phlip2005-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Railsters: > > ActiveRecord''s validation system puts other database systems to shame. > > However, the newbies might not know how to write a regexp that > excludes a match, instead of tests for it. Understand - I''m just > asking this question to help them. I have been using Regexps since > ''grep'' on Xenix! But the newbies here might not know how to do this: > > validates_format_of :field_must_break, :with => /\n/ > > validates_format_of :field_must_not_break, :with => /^[^\n]$/ > > The second one fails with data that correctly have no \n linefeed in them. > > How do I - I mean "they" - negate a regexp? > > -- > Phlip > http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!! > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Russell Norris wrote:> Are you looking for a field with only one character [which can''t be a > newline]? ''Cause that''s what /^[^\n]$/ does. You might want to add some > wildcardier metacharacters to include more possible characters than just > one.Wrong.> Or you might just want to ask this question over and over.Wrong. -- Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!! --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Um... That''s actually right. [About the friggin'' regex]. rsl@sneaky:~$ irb irb(main):001:0> rx = /^a$/ => /^a$/ irb(main):002:0> "test" =~ rx => nil # Doesn''t have an "a" irb(main):003:0> "tast" =~ rx => nil # Has an "a" but has other characters as well irb(main):004:0> "a" =~ rx => 0 # Surprise! It passes there because it has only "a" irb(main):005:0> rx = /^[^\n]$/ => /^[^\n]$/ irb(main):006:0> "\n" =~ rx => nil # Fails on returns, as expected irb(main):007:0> "" =~ rx => nil # Fails on empty string irb(main):008:0> "1" =~ rx => 0 # Passes because there''s only one character irb(main):009:0> "11" =~ rx => nil # Fails because there''s more than one character http://www.google.com/search?q=regular+expressions<http://www.google.com/search?q=regular+expressions&ie=utf-8&oe=utf-8&rls=com.ubuntu:en-US:official&client=firefox-a>much? On 2/17/07, Phlip <phlip2005-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Russell Norris wrote: > > > Are you looking for a field with only one character [which can''t be a > > newline]? ''Cause that''s what /^[^\n]$/ does. You might want to add some > > wildcardier metacharacters to include more possible characters than just > > one. > > Wrong. > > > Or you might just want to ask this question over and over. > > Wrong. > > -- > Phlip > http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!! > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 17, 2007, at 10:14 AM, Russell Norris wrote:> Um... That''s actually right. [About the friggin'' regex].Sorry, it''s not.> > rsl@sneaky:~$ irb > irb(main):001:0> rx = /^a$/ > => /^a$/ > irb(main):002:0> "test" =~ rx > => nil # Doesn''t have an "a" > irb(main):003:0> "tast" =~ rx > => nil # Has an "a" but has other characters as well > irb(main):004:0> "a" =~ rx > => 0 # Surprise! It passes there because it has only "a" > irb(main):005:0> rx = /^[^\n]$/ > => /^[^\n]$/ > irb(main):006:0> "\n" =~ rx > => nil # Fails on returns, as expected > irb(main):007:0> "" =~ rx > => nil # Fails on empty string > irb(main):008:0> "1" =~ rx > => 0 # Passes because there''s only one character > irb(main):009:0> "11" =~ rx > => nil # Fails because there''s more than one character$ irb >> r=/^a$/ => /^a$/ >> s="this\nis\na\nlong\nmulti-line\nstring" => "this\nis\na\nlong\nmulti-line\nstring" >> puts s this is a long multi-line string => nil >> s =~ r => 8 >> r2 = /^[^\n]$/ => /^[^\n]$/ >> s =~ r2 => 8 >> "what?\n!" =~ r2 => 6> http://www.google.com/search?q=regular+expressions much?No, ''cause I don''t need to.. Do you http://www.ruby-doc.org/docs/ ProgrammingRuby/html/language.html#UL ever? (got your back Philip ;-) -Rob> On 2/17/07, Phlip <phlip2005-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Russell Norris wrote: > > > Are you looking for a field with only one character [which can''t > be a > > newline]? ''Cause that''s what /^[^\n]$/ does. You might want to > add some > > wildcardier metacharacters to include more possible characters > than just > > one. > > Wrong. > > > Or you might just want to ask this question over and over. > > Wrong. > > -- > Phlip > http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Feb-17 20:24 UTC
Re: negate the regexp in validates_format_of
Hi -- On Fri, 16 Feb 2007, Phlip wrote:> > Railsters: > > ActiveRecord''s validation system puts other database systems to shame. > > However, the newbies might not know how to write a regexp that > excludes a match, instead of tests for it. Understand - I''m just > asking this question to help them. I have been using Regexps since > ''grep'' on Xenix! But the newbies here might not know how to do this: > > validates_format_of :field_must_break, :with => /\n/ > > validates_format_of :field_must_not_break, :with => /^[^\n]$/How about: /\A[^\n]+\z/ David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com) --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Well, snap on my doodle! I was so busy hoping I could give Phlip a heaping bowl of his own brand of snotty reply that I tripped on some of my own brand of over-confidence. Crow meet plate, fork, and knife. [Again.] I''m still trying to wrap my head around this. I think of "a\nb" as a string containing three characters, a, the newline character, and b. Oh, I think I see... ^ and $ don''t match the end and beginning of a _string_ but the end and beginning of a _line_... Sigh. Goddangit. Rassin'' frassin''. Oh well. Apologies to Phlip, I''m fully prepared for and deserving of your next snarky retort. [But not the one after that.] RSL On 2/17/07, Rob Biedenharn <Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org> wrote:> > > On Feb 17, 2007, at 10:14 AM, Russell Norris wrote: > > Um... That''s actually right. [About the friggin'' regex]. > > > Sorry, it''s not. > > > rsl@sneaky:~$ irb > irb(main):001:0> rx = /^a$/ > => /^a$/ > irb(main):002:0> "test" =~ rx > => nil # Doesn''t have an "a" > irb(main):003:0> "tast" =~ rx > => nil # Has an "a" but has other characters as well > irb(main):004:0> "a" =~ rx > => 0 # Surprise! It passes there because it has only "a" > irb(main):005:0> rx = /^[^\n]$/ > => /^[^\n]$/ > irb(main):006:0> "\n" =~ rx > => nil # Fails on returns, as expected > irb(main):007:0> "" =~ rx > => nil # Fails on empty string > irb(main):008:0> "1" =~ rx > => 0 # Passes because there''s only one character > irb(main):009:0> "11" =~ rx > => nil # Fails because there''s more than one character > > > $ irb > >> r=/^a$/ > => /^a$/ > >> s="this\nis\na\nlong\nmulti-line\nstring" > => "this\nis\na\nlong\nmulti-line\nstring" > >> puts s > this > is > a > long > multi-line > string > => nil > >> s =~ r > => 8 > >> r2 = /^[^\n]$/ > => /^[^\n]$/ > >> s =~ r2 > => 8 > >> "what?\n!" =~ r2 > => 6 > > > http://www.google.com/search?q=regular+expressions<http://www.google.com/search?q=regular+expressions&ie=utf-8&oe=utf-8&rls=com.ubuntu:en-US:official&client=firefox-a>much? > > > No, ''cause I don''t need to.. Do you > http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UL ever? > > (got your back Philip ;-) > > -Rob > > On 2/17/07, Phlip <phlip2005-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Russell Norris wrote: > > > > > Are you looking for a field with only one character [which can''t be a > > > newline]? ''Cause that''s what /^[^\n]$/ does. You might want to add > > some > > > wildcardier metacharacters to include more possible characters than > > just > > > one. > > > > Wrong. > > > > > Or you might just want to ask this question over and over. > > > > Wrong. > > > > -- > > Phlip > > http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!! > > > > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Feb-17 21:56 UTC
Re: negate the regexp in validates_format_of
Hi -- On Sat, 17 Feb 2007, Russell Norris wrote:> I''m still trying to wrap my head around this. I think of "a\nb" as a string > containing three characters, a, the newline character, and b. Oh, I think I > see... ^ and $ don''t match the end and beginning of a _string_ but the end > and beginning of a _line_...That''s correct. \A and \z match the absolute beginning and end of a string. \Z matches the end of a string but discounting a final newline, if present. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com) --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---