Gsub regex


#1

I’m trying to convert this:
09%20PM
To this:
13%20PM
With this piece of code:
18%20PM
BUT I want to avoid having a space appear before the dash and I’ve tried to search how to do this but to no avail.
: (


#2

You can simply just use another regex
x = "Haunted-House"
“Haunted-House”
x.gsub(/[A-Z\s]/, "_ ")
“_ _ _ _ _ _ _ _ -_____”
x.gsub(/\s[-]/, "-")
Will give you the output you want

I recommend using this tool for regex matching
Rubular


#3

I tried using both regexes and it seems that the ‘H’ in ‘HOUSE’ has been pushed over by an underscore and the letter ‘E’ is no longer available as it was also pushed over completely.
Here is the outcome:
45%20AM

Btw, I want to thank you for helping me! : )

I am very new to regex and still have not touched the topic in my studies but I decided to get a taste of it now!

Just a heads up, this is for my hangman game!


#5

x.gsub(/\s[-]/,"-")

=> “HAUNTED-HOUSE”

y = x.gsub(/[A-Z\s]/, "_ ")

=> "_ _ _ _ _ _ _ -_ _ _ _ _ "

z = y.gsub(/\s[-]/,"-")

Codecademy is mangling the output but z will output what you want