开发者

Remove the date from a string

开发者 https://www.devze.com 2023-02-01 06:23 出处:网络
Looking for some Regex help in Ruby. I have a string that is formated as so: YYYY-MM-DD-title-of-post.markdown

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

精彩评论

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