Wondering, is that a regex in powershell can replace date from dd-mon-yy to dd/MM/yy
想知道的是,powershell中的regex是否可以替换从dd-mon-yy到dd/MM/yy的日期
Example: 25-FEB-16 change to 25/02/16
示例:25-FEB-16更改为25/02/16
2 个解决方案
#1
1
You should use [DateTime]::ParseExact()
as regex would require 12 different replace-operations, or a MatchEvalutor
to convert the month.
您应该使用[DateTime]: ParseExact(),因为regex将需要12个不同的替换操作,或者一个MatchEvalutor来转换月份。
Example using regex MatchEvaluator
:
使用regex MatchEvaluator示例:
$MatchEvaluator = {
param($match)
#Could have used a switch-statement too..
$month = [datetime]::ParseExact($match.Groups[2].Value,"MMM",$null).Month
"{0:00}/{1:00}/{2:00}" -f $match.Groups[1].Value, $month, $match.Groups[3].Value
}
[regex]::Replace("25-FEB-16","(\d+)-(\w+)-(\d+)", $MatchEvaluator)
25/02/16
Looking at that, I would say that using only ParseExact()
is a much better solution:
考虑到这一点,我认为只使用ParseExact()是一个更好的解决方案:
try {
[datetime]::ParseExact("25-FEB-16","dd-MMM-yy", $null).ToString("dd/MM/yy", [cultureinfo]::InvariantCulture)
} catch {
#Invalid date-format
}
25/02/16
#2
1
The better solution here would be to use the date parsing and formatting functions rather than regex-based string replacement.
这里更好的解决方案是使用日期解析和格式化函数,而不是基于正则表达式的字符串替换。
[DateTime]::ParseExact('25-FEB-16', 'dd-MMM-yy', $null).ToString('dd/MM/yy', [System.Globalization.CultureInfo]::InvariantCulture)
# => 25/02/16
This also gets you built-in locale sensitivity in case you run on a system where the dates come in with the month names in another language. But using ParseExact
and the InvariantCulture
option means that the locale-sensitivity won't mess with your specified formats.
这也为您提供了内置的语言环境敏感性,以备您在系统上运行时使用另一种语言输入月份名称。但是使用ParseExact和constant culture选项意味着位置敏感性不会影响您指定的格式。
#1
1
You should use [DateTime]::ParseExact()
as regex would require 12 different replace-operations, or a MatchEvalutor
to convert the month.
您应该使用[DateTime]: ParseExact(),因为regex将需要12个不同的替换操作,或者一个MatchEvalutor来转换月份。
Example using regex MatchEvaluator
:
使用regex MatchEvaluator示例:
$MatchEvaluator = {
param($match)
#Could have used a switch-statement too..
$month = [datetime]::ParseExact($match.Groups[2].Value,"MMM",$null).Month
"{0:00}/{1:00}/{2:00}" -f $match.Groups[1].Value, $month, $match.Groups[3].Value
}
[regex]::Replace("25-FEB-16","(\d+)-(\w+)-(\d+)", $MatchEvaluator)
25/02/16
Looking at that, I would say that using only ParseExact()
is a much better solution:
考虑到这一点,我认为只使用ParseExact()是一个更好的解决方案:
try {
[datetime]::ParseExact("25-FEB-16","dd-MMM-yy", $null).ToString("dd/MM/yy", [cultureinfo]::InvariantCulture)
} catch {
#Invalid date-format
}
25/02/16
#2
1
The better solution here would be to use the date parsing and formatting functions rather than regex-based string replacement.
这里更好的解决方案是使用日期解析和格式化函数,而不是基于正则表达式的字符串替换。
[DateTime]::ParseExact('25-FEB-16', 'dd-MMM-yy', $null).ToString('dd/MM/yy', [System.Globalization.CultureInfo]::InvariantCulture)
# => 25/02/16
This also gets you built-in locale sensitivity in case you run on a system where the dates come in with the month names in another language. But using ParseExact
and the InvariantCulture
option means that the locale-sensitivity won't mess with your specified formats.
这也为您提供了内置的语言环境敏感性,以备您在系统上运行时使用另一种语言输入月份名称。但是使用ParseExact和constant culture选项意味着位置敏感性不会影响您指定的格式。