我有以下子字符串
var fixedDays =['20180101:V','20180102:V','20180106:W','20180107:W','20180113:W','20180114:W','20180120:W','20180121:W','20180124:V','20180127:W','20180128:W','20180203:W'
是我正在执行正则表达式的较大字符串(big_string)的一部分。 我正在尝试从此子字符串中获取所有数字:20180101,20180102 ...
这是我的代码:
sREG.Global = True
sREG.MultiLine = True
sREG.IgnoreCase = True
sREG.Pattern = "var fixedDays =\[(?:'(\d{8}):[V|W]',?)+\];"
Set sRES = sREG.Execute(big_string)
但它仅返回最后一个数字20180203。
我知道这与贪婪/懒惰的量词有关,但无法做到这一点。
答案 0 :(得分:0)
一种VBA解决方案,尽管不是Regex解决方案:
Dim fixedDays As String
fixedDays = "'20180101:V','20180102:V','20180106:W','20180107:W','20180113:W','20180114:W','20180120:W','20180121:W','20180124:V','20180127:W','20180128:W','20180203:W'"
Dim str As Variant
For Each str In Split(Replace(Replace(Replace(fixedDays, "'", ""), ":W", ""), ":V", ""), ",")
Debug.Print str
Next
答案 1 :(得分:0)
我建议使用与DisplayName发布的方法类似的方法,但前提是您必须找到匹配项:
Dim big_string As String
Dim sREG As New RegExp
Dim result As Variant
Dim sRES As MatchCollection
big_string = "var fixedDays =['20180101:V','20180102:V','20180106:W','20180107:W','20180113:W','20180114:W','20180120:W','20180121:W','20180124:V','20180127:W','20180128:W','20180203:W'];"
With sREG
.Global = True
.Pattern = "var fixedDays\s*=\s*\[((?:'\d{8}:[VW]',?)+)];"
.IgnoreCase = True
End With
Set sRES = sREG.Execute(big_string)
If sRES.Count > 0 Then
result = Split(Replace(Replace(Replace(sRES(0).SubMatches(0), "'", ""), ":V", ""), ":W", ""), ",")
End If
输出:
您实际上可以将匹配项收集到集合中。
正则表达式var fixedDays\s*=\s*\[((?:'\d{8}:[VW]',?)+)];
将与var fixedDays
匹配,其中=
被0+空格包围,然后[
,然后((?:'\d{8}:[VW]',?)+)
将被捕获到组中1带有:V
或:W
前缀的单引号8位数字,后跟可选的,
。然后,];
将被匹配。