Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > regural expression with not condition.

Reply
Thread Tools

regural expression with not condition.

 
 
Vamsi Krishna
Guest
Posts: n/a
 
      01-24-2009
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=>/^['"][ ]*['"]$/ ,n=> :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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Yaser Sulaiman
Guest
Posts: n/a
 
      01-25-2009
[Note: parts of this message were removed to make it a legal post.]

On Sat, Jan 24, 2009 at 11:05 PM, Vamsi Krishna <
http://www.velocityreviews.com/forums/(E-Mail Removed)> 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/

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      01-25-2009
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=>/^['"][ ]*['"]$/ ,n=> :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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      01-25-2009
Vamsi Krishna wrote:
> validates_format_of :name,:with=>/^['"][ ]*['"]$/ ,n=> :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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error: (infix expression) for formal "din" is not a globally static expression tomer VHDL 0 07-27-2011 11:43 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments