# Regural expression with not condition

**URL:** <https://www.ruby-forum.com/t/regural-expression-with-not-condition/158560>\
**Category:** Ruby\
**Created:** [January 24, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/regural-expression-with-not-condition/158560 "2009-01-24T21:07:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [January 24, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/regural-expression-with-not-condition/158560/1 "2009-01-24T21:07:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [January 25, 2009, 9:54am UTC](https://www.ruby-forum.com/t/regural-expression-with-not-condition/158560/2 "2009-01-25T09:54:57Z")

</div>

On Sat, Jan 24, 2009 at 11:05 PM, Vamsi K. \<  
removed\_email\_address@domain.invalid\> 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](http://www.rubular.com/) is a great tool for editing and testing regular  
expressions.

Regards,  
Yaser

---

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [January 25, 2009, 4:31pm UTC](https://www.ruby-forum.com/t/regural-expression-with-not-condition/158560/3 "2009-01-25T16:31:27Z")

</div>

Vamsi K. 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

---

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [January 25, 2009, 4:53pm UTC](https://www.ruby-forum.com/t/regural-expression-with-not-condition/158560/4 "2009-01-25T16:53:17Z")

</div>

Vamsi K. 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
