Looking for some Regex help in Ruby.
I have a string that is formated as so:
YYYY-MM-DD-title-of-post.markdown
I'm looking to use gsub
or sub
to remove the YYYY-MM-DD
po开发者_JS百科rtion of the string and do further processing to it.
I would like a Regular Expression to remove the date from the string. The date will never be in a different format.
Thanks for the help!
This should do the trick:
new_title = old_title.gsub('\d{4}-\d{2}-\d{2}','')
If you are confident that the date will ALWAYS be in the format YYYY-MM-DD and will ALWAYS appear at the beginning of the string than the following regex will work:
my_string = "YYYY-MM-DD-title-of-post.markdown"
date = my_string.match(/^(\d{4}-\d{2}-\d{2})-.*/)[1]
精彩评论