开发者

Remove the date from a string

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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜