I am new to ruby and my regex knowledge leaves a lot to be desired. I am trying to check if a string is a palindrome, but wish to ignore white space and commas.
The current code I have is
def palindrome(string)
string = string.downcase
string = string.gsub(/\d+(,)\d+//\s/ ,"")
if string.reverse == string
return true
else
return false
end
end
Any assistance here would be greatly appreciated.
but wish to ignore white space and commas
You don't need to put \d in your regex. Just replace the spaces or commas with empty string.
string = string.gsub(/[\s,]/ ,"")
The above gsub command would remove all the spaces or commas. [\s,] character class which matches a space or comma.
Another way would be to use the method String#tr:
str = "pat, \t \ntap"
str.tr(" ,\t\n", '') #=> "pattap"
Related
I am trying to receive a string of characters and change all characters aside from spaces to "*". Here is where I am:
def change_word(word)
new_word.each {|replace| replace.gsub!(/./, "*") }
new_word.to_s
new_word.join
end
I'm taking a word, adding the individual characters to an array and assigning this to a new variable, replacing everything in said array with the required symbol, changing everything in the array to a string and then joining everything in the array to output a bunch of *'s.
What I would like to do (and it's not necessary that the solution follows the previous syntax) is take all letters and replace them with *. Spaces should stay as a space, only letters should become *.
What about gsub(/\S/, '*')
It will find all non-whitespace characters and replace every one of them with *. \S is a regex character class matching non-whitespace chars (thanks #jdno).
E.g.
pry> "as12 43-".gsub(/\S/, '*')
=> "**** ***"
So in your case:
def change_word(word)
word.gsub(/\S/, '*')
end
You may also extract the regex outside of the method to optimize it a bit:
CHANGE_WORD_PATTERN = /\S/
def change_word(word)
word.gsub(CHANGE_WORD_PATTERN, '*')
end
When the first argument from the tr method of String starts with "^", then it means: everything but. So "^ " means everything but a space.
word = "12 34 rfv"
word.tr("^ ","*") # => "** ** ***"
Example code:
test = "123knh8y78hah099ajs980"
test.scan(/\d+\Z/)
As a result, I should get:
=> ["980"]
It is correct. But if add the "\n" to the end of the text, regexp return
incorrect result:
test = "123knh8y78hah099ajs980\n"
test.scan(/\d+\Z/)
=> ["980"]
Nothing changed, but the number is not the end of text. In the end of text symbol "\n".
Hot to create reqular expression to fix this problem?
You need to replace \Z with \z if you do not need to match 980 at the end of the string or before the final \n (which is matched with \Z).
See Ruby Regex reference:
\Z - Matches end of string. If string ends with a newline, it matches just before newline
\z - Matches end of string
need to be able to take the last line of a string and put it in it's own string. and then more importantly I need to be able to remove the last line of the original string that has non-whitespace characters.
Consider a string like the following (line breaks written as \n):
str = "Hello\nThere\nWorld!\n\n"
First, use String#strip to remove trailing whitespace, and use String#split to break the string into an array where each element represents one line of the string.
str = str.strip.split("\n")
#=> ["Hello", "There", "World!"]
You can then extract the last line from the last element in the array using Array#pop.
last_line = str.pop
#=> "World!"
Finally, use Array#join to re-assemble the array.
str = str.join("\n")
#=> "Hello\nThere"
I'd like to have a reqex that checks that every character is a number [0-9]+. I have tried:
'4th'=~/[\d]+/
'4th'=~/\d+/
but not working. How would I check for this?
thx
"12345" =~ /\A\d+\Z/
\A = beginning of string (not line, string)
\d+ = one or more digits
\Z = end of string (not line, string)
The simplest way is:
str !~ /\D/
I can't seem to figure out the regex pattern for matching strings only if it doesn't contain whitespace. For example
"this has whitespace".match(/some_pattern/)
should return nil but
"nowhitespace".match(/some_pattern/)
should return the MatchData with the entire string. Can anyone suggest a solution for the above?
In Ruby I think it would be
/^\S*$/
This means "start, match any number of non-whitespace characters, end"
You could always search for spaces, an then negate the result:
"str".match(/\s/).nil?
>> "this has whitespace".match(/^\S*$/)
=> nil
>> "nospaces".match(/^\S*$/)
=> #<MatchData "nospaces">
^ = Beginning of string
\S = non-whitespace character, * = 0 or more
$ = end of string
Not sure you can do it in one pattern, but you can do something like:
"string".match(/pattern/) unless "string".match(/\s/)
"nowhitespace".match(/^[^\s]*$/)
You want:
/^\S*$/
That says "match the beginning of the string, then zero or more non-whitespace characters, then the end of the string." The convention for pre-defined character classes is that a lowercase letter refers to a class, while an uppercase letter refers to its negation. Thus, \s refers to whitespace characters, while \S refers to non-whitespace.
str.match(/^\S*some_pattern\S*$/)