Kalyan Maddu
Kalyan Maddu

Reputation: 4173

Regular expression for validates_format_of

Can someone help me write the regular expression for validates_format_of.

It should fail if any one of

< > $ \ any_non_printable_character
is present.

Thanks.

Upvotes: 0

Views: 125

Answers (2)

mu is too short
mu is too short

Reputation: 434635

Something like this would work with Ruby 1.9:

/\A[^<>\\$\p{^Print}]*\z/

That will match anything that doesn't contain your bad characters and hence should work nicely in a validation. That will also match an empty string though so you could use + instead of * or add a length or present? check if you wanted to exclude ''.

Upvotes: 2

Lars Hanke
Lars Hanke

Reputation: 624

Don't know Ruby, but for Perl, ... the following would work:

if(/(:?[<>\$]|[^[:print:]])/){...}

And in Python, awk, sed, ... it would look more or less the same. So hopefully this helps with Ruby.

Upvotes: 0

Related Questions