Validate That String Contains Only Allowed Characters in Ruby

Validate that string contains only allowed characters in Ruby

This seems to be faster than all previous benchmarks (by @Eric Duminil)(ruby 2.4):

!string.match?(/[^AGHTM0-9]/) 

How can I validate a string only contains specific alpha characters in Ruby?

With regex

input = "ACGTCTTAA"
if input =~ /\A[GCTA]+\z/
# put your code here
end

It means any succession of 'G', 'C', 'T' or 'A's from the beginning to the end of the string.

If an empty string is acceptable, you could use /\A[GCTA]*\z/ instead.

With String#delete

You could also delete every 'G', 'C', 'T' and 'A's with String#delete, and check if the string becomes empty :

"C".delete("GCTA").empty? #=> true
"ACGTXXXCTTAA".delete("GCTA").empty? #=> false
"ACGTCTTAA".delete("GCTA").empty? #=> true
"".delete("GCTA").empty? #=> true

Check if string contains only permitted characters

Using positive lookahead:

pattern = /^(?=.*\D)[-\w]+$/

pattern =~ 'qwerty'
# => 0
pattern =~ 'identifier-45'
# => 0
pattern =~ 'u_-_-'
# => 0
pattern =~ '-42-'
# => 0

pattern =~ '123456'
# => nil
pattern =~ 'w&1234'
# => nil
pattern =~ 'identifier 42'
# => nil

Ruby - validate user input against allowed characters

Use /^[RGBY ]+$/ as a pattern, so that from the beginning (^) to the end ($) (not partially) whole characters are consisted of allowed characters (R, G, B, Y, or space):

>> "Z Z R Y" =~ /^[RGBY ]+$/
=> nil
>> "R R R Y" =~ /^[RGBY ]+$/
=> 0

Trying to validate string only has numbers or letters (and can contain spaces)

I'm not using RUBY, however, try this regex syntax - this would require only a-zA-Z0-9 and at least one character:

/\A[a-z0-9A-Z ]+\z/

OR this one, if string can be of a length = 0:

/\A^[a-z0-9A-Z ]*\z/

-- updated to include support for space

How to check string contains special character in ruby

special = "?<>',?[]}{=-)(*&^%$#`~{}"
regex = /[#{special.gsub(/./){|char| "\\#{char}"}}]/

You can then use the regex to test if a string contains the special character:

if some_string =~ regex

This looks a bit complicated: what's going on in this bit

special.gsub(/./){|char| "\\#{char}"}

is to turn this

"?<>',?[]}{=-)(*&^%$#`~{}"

into this:

"\\?\\<\\>\\'\\,\\?\\[\\]\\}\\{\\=\\-\\)\\(\\*\\&\\^\\%\\$\\#\\`\\~\\{\\}"

Which is every character in special, escaped with a \ (which itself is escaped in the string, ie \\ not \). This is then used to build a regex like this:

/[<every character in special, escaped>]/

Regex to validate strings having only characters (without special characters but with accented characters), blank spaces and numbers

I wrote the ^(?:[^\W_]|\s)*$ answer in the question you referred to (which actually would have been different if I'd known you wanted to allow _ and -). Not being a Ruby guy myself, I didn't realize that Ruby defaults to not using Unicode for regex matching.

Sorry for my lack of Ruby experience. What you want to do is use the u flag. That switches to Unicode (UTF-8), so accented characters are caught. Here's the pattern you want:

^[\w\s-]*$

And here it is in action at Rubular. This should do the trick, I think.

The u flag works on my original answer as well, though that one isn't meant to allow _ or - characters.

How to only allow/validate a string starting with certain characters?

You could use a custom validator.

class Postal < ApplicationRecord
validate :code_includes

private

def code_includes
["V7C","V7B"].include? postalcode
end
end

That will just check that postalcode has either of those two strings anywhere as a substring.

To check that postalcode starts with one of those two you could do

class Postal < ApplicationRecord
validate :code_starts_with

private

def code_starts_with
postalcode.start_with?("V7C","V7B")
end
end

Regex to validate string having only characters (not special characters), blank spaces and numbers

There are a couple ways of doing this. If you only want to allow ASCII word characters (no accented characters like Ê or letters from other alphabets like Ӕ or ל), use this:

/^[a-zA-Z\d\s]*$/

If you want to allow only numbers and letters from other languages for Ruby 1.8.7, use this:

/^(?:[^\W_]|\s)*$/u

If you want to allow only numbers and letters from other languages for Ruby 1.9.x, use this:

^[\p{Word}\w\s-]*$

Also, if you are planning to use 1.9.x regex with unicode support in Ruby on Rails, add this line at the beginning of your .rb file:

# coding: utf-8


Related Topics



Leave a reply



Submit