是否可以从日期字符串中删除/替换/替换连字符,以便在匹配组中仅返回数字?
测试字符串
25-11-1982
11-10-1200
我想回来
25111982
11101200
到目前为止,我拥有的:
(?<date>[d]{2}-[d]{2}-[d]{4})
返回日期 =
25-11-1982
11-10-1200
尝试链接:https://regex101.com/r/3vvYHu/2/
请看下面的例子: https://regex101.com/r/AcX8FL/1
它包含正则表达式以及替换部分。
Regex:
([d]{2})-([d]{2})-([d]{4})
替代:
$1$2$3
输入:
25-11-1982
输出:
25111982
使用语言的 replace-all 函数,并将"-"
的连字符替换为空字符串""
。
例子
蟒:
date1 = '25-11-1982'
date2 = '11-10-1200'
print(date1.replace('-', '')) # 25111982
print(date2.replace('-', '')) # 11101200
爪哇岛:
String date1 = "25-11-1982";
String date2 = "11-10-1200";
System.out.println(date1.replaceAll("-", "")); // 25111982
System.out.println(date2.replaceAll("-", "")); // 11101200
JavaScript:
let date1 = "25-11-1982";
let date2 = "11-10-1200";
console.log(date1.replaceAll("-", "")); // 25111982
console.log(date2.replaceAll("-", "")); // 11101200
.PHP:
echo str_replace('-', '', '25-11-1982'); // 25111982
您可以使用正则表达式:https://regex101.com/r/HmqLTJ/1
将"-"替换为"。
或者,由下面生成的 PHP 代码:
$re = '/(-)/m';
$str = '25-11-1982';
$subst = '';
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;