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

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

Sijo Kg wrote:

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 S.
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: [email protected]
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-----

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

Sijo Kg wrote:

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

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

Sijo

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

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 S.
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: [email protected]
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-----

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

Justin C. wrote:

h123 kk

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 S.
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: [email protected]
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-----

Am Dienstag 14 Juli 2009 12:01:23 schrieb Axel S.:

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

At 2009-07-14 06:01AM, “Axel S.” wrote:

else
puts “string does not contain a comma”
end

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

Hi Axel S.
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

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

Sebastian H. wrote:

Am Dienstag 14 Juli 2009 12:01:23 schrieb Axel S.:

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 S.
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: removed_email_[email protected]
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-----

Am Dienstag 14 Juli 2009 12:48:25 schrieb Axel S.:

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.

Why cann’t you look for , only?

like str =~ /,/

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

Thanks

On Jul 14, 4:25 am, Sijo Kg [email protected] 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?

One way: in your active record model:

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

Seems easier to read.

Hi –

On Tue, 14 Jul 2009, Glenn J. 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.

David

Hi –

On Tue, 14 Jul 2009, Axel S. wrote:

when /^[^,]$/ then puts “#{string.inspect} has no comma”
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

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

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).
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 S.
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: [email protected]
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-----

Axel S. wrote:

case string
EOM

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

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

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.

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.

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

Thanks
Jagadeesh

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs