Forum: Ruby regural expression with not condition.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
91a616ee807e220c8ffccb5f2ad33731?d=identicon&s=25 Vamsi Krishna (vamsikrishna)
on 2009-01-24 21:07
Hi all,

 i'm trying validate the format which doesn't consist of an empty string
in the single or double quotes.

validates_format_of :name,:with=>/^['"][ ]*['"]$/ ,:on=> :create

/^['"][ ]*['"]$/ it does validation for like the string ' ' and " " . i
need except this like the string may contain "vamsi"  or 'vamsi' or
"vamsi's" all these three are valid for me.

so how to modify the above expression is there any thing like
 :without? (ofcourse there is no)

 or can we put a not condition for the regular expression
 like  !(/^['"][ ]*['"]$/)


Thanks
VK.
B09f4659460545e38ece34ddd0d96b46?d=identicon&s=25 Yaser Sulaiman (Guest)
on 2009-01-25 09:54
(Received via mailing list)
On Sat, Jan 24, 2009 at 11:05 PM, Vamsi Krishna <
vamsikrishna.naidu@gmail.com> wrote:

> Hi all,
>
>  i'm trying validate the format which doesn't consist of an empty string
> in the single or double quotes.
>

Hi.

How about /^['"](.+)['"]$/.
It will match a single or double quotation mark at the start of the
line,
followed by one or more of any character, followed by a single or double
quotation mark at the end of the line.

Please note that it will match things like "'" (a single quotation mark
inside double quotes) and ''' (a single quotation mark inside single
quotes). You might want to handle such cases or come up with a better
regexp.

BTW, Rubular[1] is a great tool for editing and testing regular
expressions.

Regards,
Yaser

[1]: http://www.rubular.com/
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-25 16:31
Vamsi Krishna wrote:
>  i'm trying validate the format which doesn't consist of an empty string
> in the single or double quotes.
>
> validates_format_of :name,:with=>/^['"][ ]*['"]$/ ,:on=> :create
>
> /^['"][ ]*['"]$/ it does validation for like the string ' ' and " " . i
> need except this like the string may contain "vamsi"  or 'vamsi' or
> "vamsi's" all these three are valid for me.

What do you mean: the string may contain spaces, but must not start or
end with one?

> so how to modify the above expression is there any thing like
>  :without? (ofcourse there is no)

Yes there is:
  validates_each :name do |record,attr,value|
    record.errors.add attr, 'is invalid' if value[0] =~ /some_regexp/
  end
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-25 16:53
Vamsi Krishna wrote:
> validates_format_of :name,:with=>/^['"][ ]*['"]$/ ,:on=> :create

P.S. WARNING!! Never use ^ and $ in regular expressions which validate
untrusted data. This is because, unlike Perl, they don't mean "match
start and end of string", but match start and end of a *line* within the
string. You need \A and \z to anchor at the start and end of the string.

irb(main):001:0> bad_data = "myname\nrm -rf /*"
=> "myname\nrm -rf /*"
irb(main):002:0> puts "is valid" if bad_data =~ /^[a-z]+$/
is valid
=> nil
irb(main):003:0> puts "is valid" if bad_data =~ /\A[a-z]\z$/
=> nil
This topic is locked and can not be replied to.