我继续尝试在VBScript&中使用RegExp执行字符串格式匹配。 VB6。我现在正在尝试匹配格式为:
的短的单行字符串七个字符:
一个。六个字母数字加一个“ - ”OR
湾五个字母数字加两个“ - ”
三个数字
示例包括123456-789LM65F2
,4EF789-012XY65A5
,A2345--789AB65D0
& 23456--890JK65D0
。
RegExp模式([A-Z0-9\-]{12})([65][A-F0-9]{2})
将(1) - (3)整合在一起并找到它们。
但是,如果我尝试:
c)用模式([A-Z0-9\-]{10})([A-Z]{2})([65][A-F0-9]{2})
,
d)打破(2)和& (3)w / pattern ([A-Z0-9\-]{7})([0-9]{3})([A-Z]{2})([65][A-F0-9]{2})
或
e)用交替模式([A-Z0-9]{5}[-]{2}|[A-Z0-9]{6}[-]{1})([0-9]{3})([A-Z]{2})([65][A-F0-9]{2})
它拒绝找到任何一个。
我做错了什么?以下是运行并检查这些内容的VBScript。
' VB Script
Main()
Function Main() ' RegEx_Format_sample.vbs
'Uses two paterns, TestPttn for full format accuracy check & SplitPttn
'to separate the two desired pieces
Dim reSet, EtchTemp, arrSplit, sTemp
Dim sBoule, sSlice, idx, TestPttn, SplitPttn, arrMatch
Dim arrPttn(3), arrItems(3), idxItem, idxPttn, Msgtemp
Set reSet = New RegExp
' reSet.IgnoreCase = True ' Not using
' reSet.Global = True ' Not using
' load test case formats to check & split
arrItems(0) = "0,6 nums + 1 '-',123456-789LM65F2"
arrItems(1) = "1,6 chars + 1 '-',4EF789-012XY65A5"
arrItems(2) = "2,5 chars + 2 '-',A2345--789AB65D0"
arrItems(3) = "3,5 nums + 2 '-',23456--890JK65D0"
SplitPttn = "([A-Z0-9]{5,6})[-]{1,2}([A-Z0-9]{9})" ' split pattern has never failed to work
' load the patterns to try
arrPttn(0) = "([A-Z0-9\-]{12})([65][A-F0-9]{2})"
arrPttn(1) = "([A-Z0-9\-]{10}[A-Z]{2})([65][A-F0-9]{2})"
arrPttn(2) = "([A-Z0-9\-]{7})([0-9]{3})([A-Z]{2})([65][A-F0-9]{2})"
arrPttn(3) = "([A-Z0-9]{5}[-]{2}|[A-Z0-9]{6}[-]{1})([0-9]{3})([A-Z]{2})([65][A-F0-9]{2})"
For idxPttn = 0 To 3 ' select Test pattern
TestPttn = arrPttn(idxPttn)
TestPttn = TestPttn & "[%]" ' append % "ender" char
SplitPttn = SplitPttn & "[%]" ' append % "ender" char
For idxItem = 0 To 3
reSet.Pattern = TestPttn ' set to Test pattern
sTemp = arrItems(idxItem )
arrSplit = Split(sTemp, ",") ' arrSplit is Split array
EtchTemp = arrSplit(2) & "%" ' append % "ender" char to Item sub (2) as the "phrase" under test
If reSet.Test(EtchTemp) = False Then
MsgBox("RegEx " & TestPttn & " false for " & EtchTemp & " as " & arrSplit(1) )
Else ' test OK; now switch to SplitPttn
reSet.Pattern = SplitPttn
Set arrMatch = reSet.Execute(EtchTemp) ' run Pttn as Exec this time
If arrMatch.Count > 0 then ' If test OK then Count s/b > 0
Msgtemp = ""
Msgtemp = "RegEx " & TestPttn & " TRUE for " & EtchTemp & " as " & arrSplit(1)
For idx = 0 To arrMatch.Item(0).Submatches.Count - 1
Msgtemp = Msgtemp & Chr(13) & Chr(10) & "Split segment " & idx & " as " & arrMatch.Item(0).submatches.Item(idx)
Next
MsgBox(Msgtemp)
End If ' Count OK
End If ' test OK
Next ' idxItem
Next ' idxPttn
End Function
答案 0 :(得分:1)
试试这个正则表达式:
(?:[A-Z0-9]{6}-|[A-Z0-9]{5}--)[0-9]{3}[A-Z]{2}65[0-9A-F]{2}
<强>解释强>
(?:[A-Z0-9]{6}-|[A-Z0-9]{5}--)
- 匹配6个字母数字字符,后跟-
或5个字母数字字符,后跟--
[0-9]{3}
- 匹配3位数[A-Z]{2}
- 匹配2个字母65
- 按字面意思匹配65
[0-9A-F]{2}
- 匹配2个HEX符号您可以从以下代码中获得一些想法:
VBScript代码:
Option Explicit
Dim objReg, strTest
strTest = "123456-789LM65F2" 'Change the value as per your requirements. You can also store a list of values in an array and run the code in loop
set objReg = new RegExp
objReg.Global = True
objReg.IgnoreCase = True
objReg.Pattern = "(?:[A-Z0-9]{6}-|[A-Z0-9]{5}--)[0-9]{3}[A-Z]{2}65[0-9A-F]{2}"
if objReg.test(strTest) then
msgbox strTest&" matches with the Pattern"
else
msgbox strTest&" does not match with the Pattern"
end if
set objReg = Nothing
您的模式不起作用,因为:
([A-Z0-9\-]{12})([65][A-F0-9]{2})
- 匹配12次AlphaNumeric字符或-
后跟 6 或 5后跟2个十六进制字符
([A-Z0-9\-]{10}[A-Z]{2})([65][A-F0-9]{2})
- 匹配10个AlphaNumeric字符或-
后跟2个字母后跟 6 或 5后跟2个匹配十六进制字符
([A-Z0-9\-]{7})([0-9]{3})([A-Z]{2})([65][A-F0-9]{2})
- 匹配7个AlphaNumeric字符或-
后跟3个数字后跟2个字母后跟 6 或 5后跟2个十六进制字符
([A-Z0-9]{5}[-]{2}|[A-Z0-9]{6}[-]{1})([0-9]{3})([A-Z]{2})([65][A-F0-9]{2})
- 匹配5次出现的AlphaNumeric字符后跟--
或6次出现的字母数字后跟-
。然后是3位数后跟2个字母,接着是 6 或 5后跟2个十六进制字符
答案 1 :(得分:0)
尝试这种模式:
(([A-Z0-9]{5}--)|([A-Z0-9]{6}-))[0-9]{3}[A-Z]{2}65[0-9A-F]{2}
或者,如果最后一部分不喜欢[A-F]
(([A-Z0-9]{5}--)|([A-Z0-9]{6}-))[0-9]{3}[A-Z]{2}65[0-9ABCDEF]{2}
答案 2 :(得分:0)
所有,tanx再次为你的帮助!!
trincot,逗号之间的每个arrItems()中的所有内容,包括“加号”,仅仅是每个项目特征的简写描述的一部分,例如“5个字符加2个破折号”。
Gurman,你的pttn故障是有帮助的,但是,如果我读得对,加上了?前缀是“匹配零或一次出现”,这必须恰好匹配一次出现。此外,我的第一个模式(匹配12)实际上DID适用于我的所有测试用例。
jNevill,&amp; JMichelB你的建议与我最终的结果非常接近 我是“过度分类”。经过一些修补,我能够通过在原始的交替模式中[]中取出[65]来成功识别这些测试用例。那是我从([65])到(65)和Zammo!它奏效了。
原始图案:
([A-Z0-9]{5}[-]{2}|[A-Z0-9]{6}[-]{1})([0-9]{3})([A-Z]{2})([65][A-F0-9]{2})
Wkg图案:
([A-Z0-9]{5}[-]{2}|[A-Z0-9]{6}[-]{1})([0-9]{3})([A-Z]{2})(65)([A-F0-9]{2})
哦,我感动了
SplitPttn = SplitPttn & "[%]" ' append % "ender" char
stmt up For For ... Next循环。这有助于分裂。
T骨