从字符串中提取数字,然后将其作为日期
您好,我想提取以下字符串
“ 2020y 3m 1d 16h”
的年,月份和日期,并希望像以下输出:
“ 2020-03-01”(或“ 2020-3-1”,但是日期类型)
我尝试搜索Google,但只能得到[提取某些模式 - 大多数在标点符号中具有模式],[提取所有数字 -很难删除16等。
有人可以帮我吗?
非常感谢您!
Hello I am trying to extract Year, Month, and Date from the following string
"2020y 3m 1d 16h"
and desiring for an output like the following:
"2020-03-01" (or "2020-3-1" but a date type)
I've tried searching up Google but was only able to get [extraction with certain patterns- most of them had patterns in punctuation], [extract all the numbers - had a hard time deleting 16 etc].
Can somebody please help me out with this?
Thank you so much in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们可以首先从输入中删除“ __H”字符串,然后从
lubridate
软件包中使用ymd()
函数将其转换为日期。REGEX:
\\ s
任何白色空间(要匹配“ 16H”之前的空间)\\ d {1,2}
任何发生1至2次的数字(因为小时应从00到23或24,最大时只有两个数字)We can first remove the " __h" string from the input, then use the
ymd()
function from thelubridate
package to turn it into date.Regex:
\\s
any white space (to match the space before "16h")\\d{1,2}
any digit that occurs 1 to 2 times (since hour should range from 00 to 23 or 24, which only has two digits at max)将y,m,d字符转换为短破折线,然后使用as.posixct将其转换为DateTime类。这些空间可能不在月或日期中或以上是10个。
这也可以通过以下输入成功:
....而Benson23的答案失败。如果您打算丢弃小时信息,则格式字符串可能是:
通常应该提供更多可能的输入来支持代码测试。
Convert the y,m,d characters to short dashes and then use as.POSIXct to convert to datetime class. The spaces would possibly not be present of the month or date were at or above 10.
This also succeeds with input like:
....whereas the answer from benson23 fails. If you are intending to throw away the hours information, the format string could be:
You should generally offer a larger set of possible input to support testing of code.
strsplit
在一个或多个非数字\\ d+
上使用strsplit
之后,可以使用iSodate
。如果您将
as.date
删除,您甚至会随着时间的推移获得“ posixt”
类。数据:
You could use
ISOdate
afterstrsplit
ting the strings on one or more non-digits\\D+
.If you'd leave out the
as.Date
you even would get"POSIXt"
class with time.Data: