regural expression with not condition.

Discussion in 'Ruby' started by Vamsi Krishna, Jan 24, 2009.

  1. 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=>/^['"][ ]*['"]$/ ,:eek: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/.
    Vamsi Krishna, Jan 24, 2009
    #1
    1. Advertising

  2. [Note: parts of this message were removed to make it a legal post.]

    On Sat, Jan 24, 2009 at 11:05 PM, Vamsi Krishna <
    > 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/
    Yaser Sulaiman, Jan 25, 2009
    #2
    1. Advertising

  3. 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=>/^['"][ ]*['"]$/ ,:eek: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/.
    Brian Candler, Jan 25, 2009
    #3
  4. Vamsi Krishna wrote:
    > validates_format_of :name,:with=>/^['"][ ]*['"]$/ ,:eek: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/.
    Brian Candler, Jan 25, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    12
    Views:
    692
    Remon van Vliet
    Jun 15, 2005
  2. HockeyFan
    Replies:
    2
    Views:
    407
    HockeyFan
    Dec 7, 2006
  3. Trygve Laugstøl
    Replies:
    4
    Views:
    4,330
    rickman
    Jan 24, 2011
  4. tomer
    Replies:
    0
    Views:
    2,004
    tomer
    Jul 28, 2011
  5. Bill W.
    Replies:
    13
    Views:
    293
    Phillip Gawlowski
    May 9, 2011
Loading...

Share This Page