A regex that does not contain comma

Hi
   Could anybody please tell how to write the validates_format_of a
string(in rails) which is only valid when it does not contain a
comma.For example

The are valid

hi welcome
Hello
h123 kk
hh gg hh

The are not valid

Hello,
Hi , world
Hello,welcome

Thanks in advnce
Sijo

···

--
Posted via http://www.ruby-forum.com/.

Sijo Kg wrote:

Hi
   Could anybody please tell how to write the validates_format_of a
string(in rails) which is only valid when it does not contain a
comma.For example

The are valid

hi welcome
Hello
h123 kk
hh gg hh

The are not valid

Hello,
Hi , world
Hello,welcome

You could try sth like this:

unless ( string =~ /((?>\w+\s*(?=,))+)/ )
    do something
end

If the regexp above matches, than there's a comma somewhere in your string.

Thanks in advnce
Sijo

- --
Freundliche Grüße / Kind regards

Axel Schmalowsky
Platform Engineer

···

___________________________________

domainfactory GmbH
Oskar-Messter-Str. 33
85737 Ismaning
Germany

Mobil: +49 (0)176 / 10246727
Telefon: +49 (0)89 / 55266-356
Telefax: +49 (0)89 / 55266-222

E-Mail: aschmalowsky@df.eu
Internet: www.df.eu

Registergericht: Amtsgericht München
HRB-Nummer 150294, Geschäftsführer:
Tobia Sara Marburg, Jochen Tuchbreiter

Sijo Kg wrote:

Hi
   Could anybody please tell how to write the validates_format_of a
string(in rails) which is only valid when it does not contain a
comma.For example

The are valid

hi welcome
Hello
h123 kk
hh gg hh

The are not valid

Hello,
Hi , world
Hello,welcome

Thanks in advnce
Sijo
  
I believe

/^[^,]*$/

will do what you need, including matching empty strings. rubular.com is a good place to try out regular expressions.

-Justin

Hi
  Thanks for the reply..And I tried in the rails project like

validates_format_of :name, :with => /((?>\w+\s*(?=,))+)/

         And what I expect is if name has a comma anywhere validation
fails .Could you please tel how to get that?

Sijo

···

--
Posted via http://www.ruby-forum.com/.

Thanks Justin for the reply. It worked.
Thanks to Axel Schmalowsky also

Sijo

···

--
Posted via http://www.ruby-forum.com/.

Justin Collins wrote:

Sijo Kg wrote:

Hi
   Could anybody please tell how to write the validates_format_of a
string(in rails) which is only valid when it does not contain a
comma.For example

The are valid

hi welcome
Hello
h123 kk
hh gg hh

The are not valid

Hello,
Hi , world
Hello,welcome

Thanks in advnce
Sijo

I believe

/^[^,]*$/

will do what you need, including matching empty strings. rubular.com is
a good place to try out regular expressions.

-Justin

I'm confused. Trying the regexp /^[^,]*$/ fails with the code below:

#! /opt/csw/bin/ruby

string = "Hello, world"
#regexp = /\w+(?>\s*(?=,))+/
regexp = /^[^,]*$/

if string =~ regexp
    puts "string contains a comma"
else
    puts "string does not contain a comma"
end

p string =~ regexp

Am I missing sth?

- --
Freundliche Grüße / Kind regards

Axel Schmalowsky
Platform Engineer

···

___________________________________

domainfactory GmbH
Oskar-Messter-Str. 33
85737 Ismaning
Germany

Mobil: +49 (0)176 / 10246727
Telefon: +49 (0)89 / 55266-356
Telefax: +49 (0)89 / 55266-222

E-Mail: aschmalowsky@df.eu
Internet: www.df.eu

Registergericht: Amtsgericht München
HRB-Nummer 150294, Geschäftsführer:
Tobia Sara Marburg, Jochen Tuchbreiter

Sijo Kg wrote:

Hi
  Thanks for the reply..And I tried in the rails project like

validates_format_of :name, :with => /((?>\w+\s*(?=,))+)/

         And what I expect is if name has a comma anywhere validation
fails .Could you please tel how to get that?

Sijo

This is how I tested it:

#! /opt/csw/bin/ruby

string = "Hello world"
regexp = /(?>\w+\s*(?=,))+/

if string =~ regexp
    puts "string contains a comma (validation failed)"
else
    puts "string does not contain a comma (validated)"
end

p string =~ regexp

Hope that helps

- --
Freundliche Grüße / Kind regards

Axel Schmalowsky
Platform Engineer

···

___________________________________

domainfactory GmbH
Oskar-Messter-Str. 33
85737 Ismaning
Germany

Mobil: +49 (0)176 / 10246727
Telefon: +49 (0)89 / 55266-356
Telefax: +49 (0)89 / 55266-222

E-Mail: aschmalowsky@df.eu
Internet: www.df.eu

Registergericht: Amtsgericht München
HRB-Nummer 150294, Geschäftsführer:
Tobia Sara Marburg, Jochen Tuchbreiter

Hi Axel Schmalowsky
  What I need is if there is any comma in the string it should fail and
otherwise success .And that is what /^[^,]*$/ gives I think you too
correct but in the reverse sense

Sijo

···

--
Posted via http://www.ruby-forum.com/.

Your code expects the regex to match if the string contains a comma and not
match if the string does not contains a comma. That's the opposite of what
Justin's regex does and also the opposite of what the Sijo asked for.
Also if the goal had been to have the regex match if there is a comma /,/
would have sufficed as a regex.
Also note that with your regexp your code outputs "string does not contain a
comma" for the string ",".

HTH,
Sebastian

···

Am Dienstag 14 Juli 2009 12:01:23 schrieb Axel Schmalowsky:

Trying the regexp /^[^,]*$/ fails with the code below:
[...]
Am I missing sth?

case string
    when /,/ then puts "#{string.inspect} contains a comma"
    when /^[^,]*$/ then puts "#{string.inspect} has no comma"
    end

···

At 2009-07-14 06:01AM, "Axel Schmalowsky" wrote:

I'm confused. Trying the regexp /^[^,]*$/ fails with the code below:

#! /opt/csw/bin/ruby

string = "Hello, world"
#regexp = /\w+(?>\s*(?=,))+/
regexp = /^[^,]*$/

if string =~ regexp
     puts "string contains a comma"
else
     puts "string does not contain a comma"
end

--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous

One way: in your active record model:

def validate
  errors.add(:name, "has invalid format") if name.include? ","
end

Seems easier to read.

···

On Jul 14, 4:25 am, Sijo Kg <s...@maxxion.com> wrote:

Hi
Thanks for the reply..And I tried in the rails project like

validates_format_of :name, :with => /((?>\w+\s*(?=,))+)/

     And what I expect is if name has a comma anywhere validation

fails .Could you please tel how to get that?

Sebastian Hungerecker wrote:

Trying the regexp /^[^,]*$/ fails with the code below:
[...]
Am I missing sth?

Your code expects the regex to match if the string contains a comma and

not

match if the string does not contains a comma. That's the opposite of what
Justin's regex does and also the opposite of what the Sijo asked for.
Also if the goal had been to have the regex match if there is a comma /,/
would have sufficed as a regex.
Also note that with your regexp your code outputs "string does not

contain a

comma" for the string ",".

HTH,
Sebastian

Well, negative logic .. :wink:

This regexp matches correctly if the string consists only of a comma:

/(?>(?:\w+\s*)*(?=,))+/

- --
Freundliche Grüße / Kind regards

Axel Schmalowsky
Platform Engineer

···

Am Dienstag 14 Juli 2009 12:01:23 schrieb Axel Schmalowsky:

___________________________________

domainfactory GmbH
Oskar-Messter-Str. 33
85737 Ismaning
Germany

Mobil: +49 (0)176 / 10246727
Telefon: +49 (0)89 / 55266-356
Telefax: +49 (0)89 / 55266-222

E-Mail: aschmalowsky@df.eu
Internet: www.df.eu

Registergericht: Amtsgericht München
HRB-Nummer 150294, Geschäftsführer:
Tobia Sara Marburg, Jochen Tuchbreiter

Hi --

   case string
   when /,/ then puts "#{string.inspect} contains a comma"
   when /^[^,]*$/ then puts "#{string.inspect} has no comma"
   end

That second regex, on its own, won't tell you the whole story:

   string = <<-EOM
   Hi.
   I have, at a minimum, two commas.
   Bye.
   EOM

   p "Match!" if /^[^,]*$/.match(string)
     => Match!

You'd want to use \A and \z, rather than ^ and $ (which delimit lines,
rather than the whole string).

But if you've already tested for /,/, then you should be able just to
have an else clause in your case statement, to cover cases where /,/
didn't match.

David

···

On Tue, 14 Jul 2009, Glenn Jackman wrote:

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN)

Why cann't you look for , only?

like str =~ /,/

You do not need to looks for what other characters are.

Thanks

···

On Jul 14, 3:48 pm, Axel Schmalowsky <aschmalow...@df.eu> wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sebastian Hungerecker wrote:
> Am Dienstag 14 Juli 2009 12:01:23 schrieb Axel Schmalowsky:
>> Trying the regexp /^[^,]*$/ fails with the code below:
>> [...]
>> Am I missing sth?

> Your code expects the regex to match if the string contains a comma and
not
> match if the string does not contains a comma. That's the opposite of what
> Justin's regex does and also the opposite of what the Sijo asked for.
> Also if the goal had been to have the regex match if there is a comma /,/
> would have sufficed as a regex.
> Also note that with your regexp your code outputs "string does not
contain a
> comma" for the string ",".

> HTH,
> Sebastian

Well, negative logic .. :wink:

This regexp matches correctly if the string consists only of a comma:

/(?>(?:\w+\s*)*(?=,))+/

- --
Freundliche Grüße / Kind regards

Axel Schmalowsky
Platform Engineer
___________________________________

domainfactory GmbH
Oskar-Messter-Str. 33
85737 Ismaning
Germany

Mobil: +49 (0)176 / 10246727
Telefon: +49 (0)89 / 55266-356
Telefax: +49 (0)89 / 55266-222

E-Mail: aschmalow...@df.eu
Internet:www.df.eu

Registergericht: Amtsgericht München
HRB-Nummer 150294, Geschäftsführer:
Tobia Sara Marburg, Jochen Tuchbreiter
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (MingW32)
Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org

iEYEARECAAYFAkpcYm8ACgkQsuqpduCyZM1itQCfZDT+img/utF3v72MxZFIcNTO
gTMAn3mf1/4dIyQdqv5XQz16jyD1j3Sr
=rY8S
-----END PGP SIGNATURE-----

Well, negative logic .. :wink:

Wich does not work if you pass the regexp as an argument to a method instead
of using it in a conditional.

This regexp matches correctly if the string consists only of a comma:

/(?>(?:\w+\s*)*(?=,))+/

So does /,/. Honestly, what's wrong with /,/? Or /(?=,)/ if you really need it
to not consume the match, though I don't see why you would.

···

Am Dienstag 14 Juli 2009 12:48:25 schrieb Axel Schmalowsky:

David A. Black wrote:

Hi --

   case string
   when /,/ then puts "#{string.inspect} contains a comma"
   when /^[^,]*$/ then puts "#{string.inspect} has no comma"
   end

That second regex, on its own, won't tell you the whole story:

   string = <<-EOM
   Hi.
   I have, at a minimum, two commas.
   Bye.
   EOM

   p "Match!" if /^[^,]*$/.match(string)
     => Match!

Technically, the regexp above always succeeds (iirc).
Even though the regexp is delimited by ^ and $,
it matches always as along as the string against which the regexp is applied
does not consist of only a single comma ('[^,]* -- match everything
(including nothingness) but a comma).

So, I guess it's better to simply use /,/.

You'd want to use \A and \z, rather than ^ and $ (which delimit lines,
rather than the whole string).

But if you've already tested for /,/, then you should be able just to
have an else clause in your case statement, to cover cases where /,/
didn't match.

David

- --
Freundliche Grüße / Kind regards

Axel Schmalowsky
Platform Engineer

···

On Tue, 14 Jul 2009, Glenn Jackman wrote:

___________________________________

domainfactory GmbH
Oskar-Messter-Str. 33
85737 Ismaning
Germany

Mobil: +49 (0)176 / 10246727
Telefon: +49 (0)89 / 55266-356
Telefax: +49 (0)89 / 55266-222

E-Mail: aschmalowsky@df.eu
Internet: www.df.eu

Registergericht: Amtsgericht München
HRB-Nummer 150294, Geschäftsführer:
Tobia Sara Marburg, Jochen Tuchbreiter

Yes, I could have used the else clause, but I wanted to explicitly spell
out the "negation" of /,/ in case the OP required it.

Coming from Tcl and Perl, and being used to line-wise parsing, I wasn't
fully aware of the difference between ^$ and \A\Z

Thanks for learnin' me.

···

At 2009-07-14 08:54AM, "David A. Black" wrote:

Hi --

On Tue, 14 Jul 2009, Glenn Jackman wrote:

> case string
> when /,/ then puts "#{string.inspect} contains a comma"
> when /^[^,]*$/ then puts "#{string.inspect} has no comma"
> end

That second regex, on its own, won't tell you the whole story:

    string = <<-EOM
    Hi.
    I have, at a minimum, two commas.
    Bye.
    EOM

    p "Match!" if /^[^,]*$/.match(string)
      => Match!

You'd want to use \A and \z, rather than ^ and $ (which delimit lines,
rather than the whole string).

But if you've already tested for /,/, then you should be able just to
have an else clause in your case statement, to cover cases where /,/
didn't match.

--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous

Hi --

···

On Tue, 14 Jul 2009, Axel Schmalowsky wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

David A. Black wrote:

Hi --

On Tue, 14 Jul 2009, Glenn Jackman wrote:

   case string
   when /,/ then puts "#{string.inspect} contains a comma"
   when /^[^,]*$/ then puts "#{string.inspect} has no comma"
   end

That second regex, on its own, won't tell you the whole story:

   string = <<-EOM
   Hi.
   I have, at a minimum, two commas.
   Bye.
   EOM

   p "Match!" if /^[^,]*$/.match(string)
     => Match!

Technically, the regexp above always succeeds (iirc).
Even though the regexp is delimited by ^ and $,
it matches always as along as the string against which the regexp is applied
does not consist of only a single comma ('[^,]* -- match everything
(including nothingness) but a comma).

So, I guess it's better to simply use /,/.

Ha -- yes, it does indeed always match. I was too focused on the ^$
vs. \A\z thing to pick up on the * thing :slight_smile:

David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN)

Axel Schmalowsky wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
David A. Black wrote:
  

Hi --

   case string
   when /,/ then puts "#{string.inspect} contains a comma"
   when /^[^,]*$/ then puts "#{string.inspect} has no comma"
   end
      

That second regex, on its own, won't tell you the whole story:

   string = <<-EOM
   Hi.
   I have, at a minimum, two commas.
   Bye.
   EOM

   p "Match!" if /^[^,]*$/.match(string)
     => Match!

Technically, the regexp above always succeeds (iirc).
Even though the regexp is delimited by ^ and $,
it matches always as along as the string against which the regexp is applied
does not consist of only a single comma ('[^,]* -- match everything
(including nothingness) but a comma).

So, I guess it's better to simply use /,/.
  
The OP needed something that would match only if there were no commas. I am not a Rails person, but I looked up the method mentioned by the OP and it is indeed expecting a regex that matches valid input (e.g., no commas) and rejects invalid input (any commas). You are right about the line begin/end, though. If there could be multiple lines in the input, it should use \A and \z instead of ^ and $. I guess I am just too used to parsing things one line at a time :slight_smile:

-Justin

···

On Tue, 14 Jul 2009, Glenn Jackman wrote:

David A. Black wrote:
> string = <<-EOM
> Hi.
> I have, at a minimum, two commas.
> Bye.
> EOM
>
> p "Match!" if /^[^,]*$/.match(string)
> => Match!
>
Technically, the regexp above always succeeds (iirc).

Always? Only if a newline appears before the first comma.

···

At 2009-07-14 09:08AM, "Axel Schmalowsky" wrote:

Even though the regexp is delimited by ^ and $,
it matches always as along as the string against which the regexp is applied
does not consist of only a single comma ('[^,]* -- match everything
(including nothingness) but a comma).

--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous