[英]RegEx to extract Date from time
I have the following date & time and looking to convert it to a simple digit using RegEx 我有以下日期和时间,希望使用RegEx将其转换为简单数字
What I have: 2017-11-02T04:00:00Z 我所拥有的:2017-11-02T04:00:00Z
What I want to convert it to: 20171102 (including leading zeros of months/dates) 我想将其转换为:20171102(包括月份/日期的前导零)
Is this possible with RegEx? RegEx有可能吗? Was able to only get '2017-' after hours of work using '^(.*?)-'
工作小时后只能使用'^(。*?)-'获得'2017-'
Seems harder and harder, any helping hand is much appreciated 似乎越来越难,任何帮助之手都值得赞赏
Edit: Showing where I am trying this. 编辑:显示我在哪里尝试。 I am trying to do this on Nintex for SharePoint.
我正在尝试在Nintex for SharePoint上执行此操作。 But prior to that, I am using www.regEx101.com to validate
但在此之前,我使用www.regEx101.com进行验证
How about ^\\d{4}-\\d{2}-\\d{2}
? ^\\d{4}-\\d{2}-\\d{2}
怎么样?
This will match exactly four digits, followed by a hyphen, followed by 2 digits, followed by another hyphen, followed by another 2 digits. 这将完全匹配四个数字,后跟一个连字符,然后是2位数字,再是另一个连字符,再是另外2位数字。
var str = "2017-11-02T04:00:00Z"; var res = str.match(/^\\d{4}-\\d{2}-\\d{2}/)[0]; console.log(res);
Use the following regex with the Replace template: 将以下正则表达式与“ 替换”模板一起使用:
^(\d{4})-(\d{2})-(\d{2}).*
and provide 并提供
$1$2$3
as the replacement pattern. 作为替换模式。
Details 细节
^
- start of string ^
-字符串的开头 (\\d{4})
- Group 1 (later referenced to with $1
backreference from the replacement pattern): four digits (\\d{4})
-第1组(后继替换模式中以$1
反向引用来引用):四位数 -
- a hyphen -
连字符 (\\d{2})
- Group 1 (later referenced to with $2
backreference from the replacement pattern): two digits (\\d{2})
-第1组(后来从替换模式中以$2
反向引用来引用):两位数 -
- a hyphen -
连字符 (\\d{2})
- Group 3 (later referenced to with $3
backreference from the replacement pattern): two digits (\\d{2})
-第3组(后跟替换模式中的$3
反向引用进行引用):两位数 .*
- the rest of the string. .*
-字符串的其余部分。 See the regex demo . 参见regex演示 。
There is no way to exclude characters from a match group in regex, so you have to use three match groups. 无法从正则表达式的匹配组中排除字符,因此您必须使用三个匹配组。 I use command line sed to experiment with regex.
我使用命令行sed来测试正则表达式。 Here is some output to show what I mean:
这是一些输出,以显示我的意思:
$ echo '2017-11-02T04:00:00Z'|sed 's/^\([0-9]*\)-\([0-9]*\)-\([0-9]*\)T.*$/\1\2\3/'
20171102
This command matches each sequence of an arbitrary length of numbers within a separate capture group, and then replaces the entire string with the three groups. 此命令匹配一个单独的捕获组中任意长度的数字序列,然后将整个字符串替换为三个组。 How your language implements this functionality will vary.
您的语言如何实现此功能将有所不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.