I have a string like this:
string1 = ",,"
I want to check if there are only commas in my string. Each time string1 changes, it can have any number of commas. How can I check this?
A regex:
Start of string: \A
Comma: , (since it has no special meaning in regexes)
0+ occurrences of the previous matcher: *
End of string: \z
Not \Z! That one's intended to ignore \n at the end, such as the result of readline
The condition is:
/\A,*\z/ =~ your_string
You can find number of ,s in string1 using this:
noc = string1.scan(/,/).size
# => 2
using this, you can verify if the string contains only ,s by doing something like this:
string1=",,"
string1.scan(/,/).size == string1.size
# true
string1=",1,"
string1.scan(/,/).size == string1.size
# false
Use negative range:
",," !~ /[^,]/
# => true
Just out of curiosity:
string1.tr(',', '').empty?
string1.delete(',').empty?
(string1.split('') - [',']).empty?
string1.codepoints.uniq == [44]
def all_commas? str
str.squeeze == ','
end
all_commas? ',' #=> true
all_commas? ',,,' #=> true
all_commas? '3,' #=> false
all_commas? ' ,,,' #=> false
all_commas? ',9,,' #=> false
I'd use this :
string.each_char.all? { |c| c == ',' }
# && !string.empty? if the empty string is not valid
I think it's pretty expressive
try this
string1=",,"
if string1.count(',,') == string1.length
#here is your code
end
Related
What is the most elegant way to remove any trailing '?' or '!' from a string? The string may have any combination of trailing '?' or '!' or none at all. I have the following (which works) but it seems ugly to me. Is there a more Ruby-way?
t = 'what are you doing?!'
last_char = t[-1]
while last_char == '!' or last_char == '?'
t.chop!
last_char = t[-1]
end
'what are you doing?!'.sub(/[?!]+\z/, '')
#⇒ "what are you doing"
\z in the regular expression is the marker of the string’s end.
str = 'what are ?! you doing?!'
i = str.rindex(/[^?!]/)
#=> 20
i ? str[0..i] : ''
# => "what are ?! you doing"
str = '?!?!'
i = str.rindex(/[^?!]/)
#=> nil
i ? str[0..i] : ''
# => ""
See String#rindex.
I am supposed to get a string and make sure that it represents a binary number like "101011".
This question deals with hexadecimal strings, but I do not know how I should replace the letter H in the regex in statement !str[/\H/]. Could you help me?
A straightforward way using regex is:
"101011" !~ /[^01]/
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
Here is one way of doing this:
"101011".chars.all? {|x| x =~ /[01]/} # true if binary, else false
How can I check if an entered input only has special characters? I tried the following, but its not working:
/^[\p{L}\s\p{N}._#?¿!¡€-]+$/
"!##$%^&()!#" !~ /\w/ # => true
"!a##$%^&()!#" !~ /\w/ # => false
What about this?:
/^[^A-Za-z0-9]+$/
The pattern matches from the beginning to the end of the string and allows one or more characters which are not a letter or a number.
This will match anything that contains only non-word characters (anything but alpha-numeric)
/^[\W]+$/
//edit it also doesn't match _
To check whether an input contains only digits and letters from any alphabet, one might use \p{Alnum} matcher
▶ '¡Hello!' !~ /^\p{Alnum}+$/
#=> false
▶ 'Hello' !~ /^\p{Alnum}+$/
#=> true
▶ '¿Привет?' !~ /^\p{Alnum}+$/
#=> false
▶ 'Привет' !~ /^\p{Alnum}+$/
#=> true
That said, to check for non-alphanumerics:
▶ not '!##$%^&()!#' !~ /^[^\p{Alnum}]+$/
#=> true
▶ not 'a!##$%^&()!#' !~ /^[^\p{Alnum}]+$/
#=> false
I would like to verify a string containing repeated substrings. The substrings have a particular structure. Whole string has a particular structure (substring split by "|"). For instance, the string can be:
1=23.00|6=22.12|12=21.34|112=20.34
1=23.00|6=22.12|12=21.34
1=23.00|12=21.34
1=23.00**
How can I check that all repeated substrings match a regexp? I tried to check it with:
"1=23.00|6=22.12|12=21.34".match(/([1-9][0-9]*[=][0-9\.]+)+/)
But checking gives true even when several substrings do not match the regexp:
"1=23.00|6=ass|=21.34".match(/([1-9][0-9]*[=][0-9\.]+)+/)
# => #<MatchData "1=23.00" 1:"1=23.00">
The question is whether every repeated substring matches a regex. I understand that the substrings are separated by the character | or $/, the latter being the end of a line. We first need to obtain the repeated substrings:
a = str.split(/[#{$/}\|]/)
.map(&:strip)
.group_by {|s| s}
.select {|_,v| v.size > 1 }
.keys
Next we specify whatever regex you wish to use. I am assuming it is this:
REGEX = /[1-9][0-9]*=[1-9]+\.[0-9]+/
but it could be altered if you have other requirements.
As we wish to determine if all repeated substrings match the regex, that is simply:
a.all? {|s| s =~ REGEX}
Here are the calculations:
str =<<_
1=23.00|6=22.12|12=21.34|112=20.34
1=23.00|6=22.12|12=21.34
1=23.00|12=21.34
1=23.00**
_
c = str.split(/[#{$/}\|]/)
#=> ["1=23.00", "6=22.12", "12=21.34", "112=20.34", "1=23.00",
# "6=22.12", "12=21.34", "1=23.00", "12=21.34", "1=23.00**"]
d = c.map(&:strip)
# same as c, possibly not needed or not wanted
e = d.group_by {|s| s}
# => {"1=23.00" =>["1=23.00", "1=23.00", "1=23.00"],
# "6=22.12" =>["6=22.12", "6=22.12"],
# "12=21.34" =>["12=21.34", "12=21.34", "12=21.34"],
# "112=20.34"=>["112=20.34"], "1=23.00**"=>["1=23.00**"]}
f = e.select {|_,v| v.size > 1 }
#=> {"1=23.00"=>["1=23.00", "1=23.00" , "1=23.00"],
# "6=22.12"=>["6=22.12", "6=22.12"],
# "12=21.34"=>["12=21.34", "12=21.34", "12=21.34"]}
a = f.keys
#=> ["1=23.00", "6=22.12", "12=21.34"]
a.all? {|s| s =~ REGEX}
#=> true
This will return true if there are any duplicates, false if there are not:
s = "1=23.00|6=22.12|12=21.34|112=20.34|3=23.00"
arr = s.split(/\|/).map { |s| s.gsub(/\d=/, "") }
arr != arr.uniq # => true
If you want to resolve it through regexp (not ruby), you should match whole string, not substrings. Well, I added [|] symbol and line ending to your regexp and it should works like you want.
([1-9][0-9]*[=][0-9\.]+[|]*)+$
Try it out.
How do I add a apostrophe at the beginning and end of a string?
string = "1,2,3,4"
I would like that string to be:
'1','2','3','4'
Not sure, if this is what you want:
>> s = "1,2,3,4"
>> s.split(',').map { |x| "'#{x}'" }.join(',')
=> "'1','2','3','4'"
result = []
"1,2,3,4".split(',').each do |c|
result << "'#{c.match /\d+/}'"
end
puts result.join(',')
'1','2','3','4'
We can use regular expression to find digits
string = "1,2,3,4"
string.gsub(/(\d)/, '\'\1\'')
#=> "'1','2','3','4'"
str.insert(0, 'x')
str.insert(str.length, 'x')
After seeing your edit.
q = "1,2,3,4"
ar = q.split(',')
ar.each{|i| i.insert(0, "'").insert(-1, "'")}
q = ar.join(',')