a regex that does not contain comma

S

Sijo Kg

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
 
A

Axel Schmalowsky

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

Sijo said:
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: (e-mail address removed)
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

iEYEARECAAYFAkpcMT4ACgkQsuqpduCyZM1tjgCgujiBTLeyswnRISX4aG1iCdlm
8GYAnAkdjV2Oc8XiKvzjlWMaVnLsCN7d
=5vUO
-----END PGP SIGNATURE-----
 
S

Sijo Kg

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
 
A

Axel Schmalowsky

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

Sijo said:
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: (e-mail address removed)
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

iEYEARECAAYFAkpcRnIACgkQsuqpduCyZM2aoQCgxreJYu9uST9u884FVvi6WIAS
EnYAni1qT+62Ltw4iPoUyXcB2qGIsHAh
=JTm5
-----END PGP SIGNATURE-----
 
J

Justin Collins

Sijo said:
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
 
A

Axel Schmalowsky

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

Justin said:
Sijo said:
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: (e-mail address removed)
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

iEYEARECAAYFAkpcV28ACgkQsuqpduCyZM0AWACfbKcs4qyFyg0jCQw4zVhvqtOI
jIUAoLIYD/CNaIGrE1EaScWMQo/0F/HL
=78tH
-----END PGP SIGNATURE-----
 
S

Sijo Kg

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
 
S

Sebastian Hungerecker

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
 
A

Axel Schmalowsky

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

Sebastian said:
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 .. ;-)

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: (e-mail address removed)
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-----
 
J

Jagadeesh

Why cann't you look for , only?

like str =~ /,/

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

Thanks

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



Sebastian said:
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 .. ;-)

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:   (e-mail address removed)
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-----
 
S

Sebastian Hungerecker

Am Dienstag 14 Juli 2009 12:48:25 schrieb Axel Schmalowsky:
Well, negative logic .. ;-)

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.
 
M

Mark Thomas

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?

One way: in your active record model:

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

Seems easier to read.
 
D

David A. Black

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

--
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)
 
A

Axel Schmalowsky

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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
___________________________________

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: (e-mail address removed)
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

iEYEARECAAYFAkpcgykACgkQsuqpduCyZM042ACg3fdmTo01wS6DS6bT+416JE2/
deEAoMyfbm2wp81cKDtk7wNp9xXgnzcP
=dFl8
-----END PGP SIGNATURE-----
 
D

David A. Black

Hi --

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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 /,/.

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


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)
 
J

Justin Collins

Axel said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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 :)

-Justin
 
J

Jagadeesh

Everytime regex is a costlier operation. So for this task why can not
you use index() or similar function?

Thanks
Jagadeesh
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top