How do I check if a string has at least one number in it using Ruby?
You can use the String
class's =~
method with the regex /\d/
as the argument.
Here's an example:
s = 'abc123'
if s =~ /\d/ # Calling String's =~ method.
puts "The String #{s} has a number in it."
else
puts "The String #{s} does not have a number in it."
end
How to check if given username has at least one letter in ruby
As I understand you want to determine if the string has only letters and numbers and at least one letter. You could use the following regular expression:
r = /\A\p{Alnum}*\p{L}\p{Alnum}*\z/
This reads, "match a start-of-string anchor, followed by zero or more alphanumeric (Unicode) characters (letters or numbers), followed by a letter, followed by zero or more alphanumeric characters, followed by an end-of-string anchor".
"12abc34".match?(r) #=> true
"1234567".match?(r) #=> false
"=12abc3".match?(r) #=> false
"".match?(r) #=> false
Another way:
r = /\A(?=.*\p{L})\p{Alnum}*\z/
This reads, "match a start-of-string anchor, followed by a letter preceded by zero or more of characters, in a positive lookahead (which consumes no characters), followed by zero or more alphanumeric characters, followed by an end-of-string anchor".
Check if a string contains only digits in ruby
You can try the following
def check_string(string)
string.scan(/\D/).empty?
end
It would be truthy if string contains only digits or if it is an empty string. Otherwise returns false.
Is there a nice way to check if a string contains at least one string from an array of strings?
arrays_of_strings_to_check_against.map{ |o| string_1 =~ /\b#{Regexp.escape(o)}\b/ }.any?
Or even:
arrays_of_strings_to_check_against.any?{ |o| string_1 =~ /\b#{Regexp.escape(o)}\b/ }
In Ruby, can I check if a string contains a letter without using regular expressions?
Try this
str.count("a-zA-Z") > 0
The count
function accepts character sets as argument.
This might still fail with ArgumentError: invalid byte sequence in UTF-8
though. If your input is invalid there is likely no way around fixing the encoding.
NB, this scans the entire string, but so does downcase
. For a performance benchmark see Eric's answer, performance varies a lot between worst case and best case scenario. As always though, readability comes before premature optimization.
Check if string has only `0` and `1`
Four ways using String
methods:
str1 = '100100110'
str2 = '100100210'
String#delete
str1.delete('01') == '' #=> true
str2.delete('01') == '' #=> false
String#tr
str1.tr('01','') == '' #=> true
str2.tr('01','') == '' #=> false
String#gsub
str1.gsub(/[01]/,'') == '' #=> true
str2.gsub(/[01]/,'') == '' #=> false
String#count
str1.count('01') == str1.size #=> true
str2.count('01') == str2.size #=> false
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
Related Topics
How to Mark a Cucumber Scenario as Pending
Rails: Methods Shared by Multiple Controllers
Remove Subdomain from String in Ruby
Obtaining a Facebook Auth Token for a Command-Line (Desktop) Application
Error in Celluloid Gem Installation
How to Find Current Abstract Route in Rails Middware
Erroneous "Insecure World Writable Dir Foo in Path" When Running Ruby Script
Interested in What the "<<" Does
How to Strip Leading and Trailing Quote from String, in Ruby
Slicing Params Hash for Specific Values
If 'Main' Is an Instance of 'Object', Why Can't I Call It
Inherit Class-Level Instance Variables in Ruby
Capistrano and API Keys in Env Variables
Rails/Ruby Error When Creating Database: Unable to Load the Eventmachine C Extension