有人能想出一个关于如何确定计划时间是否有2次的正则表达式吗?
示例:
12:01 AM-3:00 AM 4:00 AM-9:00 AM
12:00 AM-5:00 AM 10:00 AM-2:00 PM
4:00 PM-7:00 PM
10:30 AM-4:00 PM
2:00 PM-5:00 PM 6:01 PM-8:01 PM
5:00 PM-6:30 PM 7:01 PM-9:46 PM
4:00 PM-10:45 PM
我最终要做的是取出PM和AM之间的空格,并且如果它有超过1次这样的时间得到两次 - 放置 | 在有1次以上的时间之间,小写AM / PM并最后删除之间的空格:
12:01pm-3:00am|4:00am-9:00am
12:00am-5:00am|10:00am-2:00pm
4:00pm-7:00pm
10:30am-4:00pm
2:00pm-5:00pm|6:01pm-8:01pm
5:00pm-6:30pm|7:01pm-9:46pm
4:00pm-10:45pm
我目前无法想到如何在不弄乱第二组的情况下去做。我总能做到:
Regex.Replace(myTimeStr, "\s", "").toLower
但是,如果有超过1个的话,那么组合两次......
答案 0 :(得分:1)
我认为最好在这里使用两个替换。你可以这样做
Dim pattern1 As String = "(?<=\d)\s+(?=[AP])"
Dim pattern2 As String = "(?<=M)\s+(?=\d)"
Dim rgx1 As New Regex(pattern1)
Dim rgx2 As New Regex(pattern2)
Dim input As String = "12:01 AM-3:00 AM 4:00 AM-9:00 AM 4:00 AM-9:00 AM"
Dim result As String = rgx2.Replace(rgx1.Replace(input, ""), "|").toLower
<强> Ideone Demo 强>