获得这一系列正则表达式匹配的简单方法是什么?

时间:2016-10-25 23:38:45

标签: regex vba excel-vba excel

我有一个以星号有界的子串开头的字符串,例如,

*AB**XY**DZ*There is more text

我想获得所有这些子串的数组;在这种情况下,数组将如下:

[1] AB
[2] XY
[3] DZ

以下字符串不应返回任何匹配项:

There is *AB**XY**DZ* more text
There is more text *AB**XY**DZ*

我已经通过使用正则表达式捕获整个星号边界组来实现此功能,然后处理该结果以获取子字符串,如下所示:

Public Function getCodeArray(commentString As String) As Variant

Dim codeString As String
Dim mCol As MatchCollection
Dim regex As RegExp
Dim delimiter As String

Set regex = New RegExp
regex.Pattern = "^([*][A-Za-z]{2}[*])+"
delimiter = "|"
Set mCol = regex.Execute(commentString)
codeString = mCol(0).Value    
codeString = Replace(codeString, "**", delimiter)
codeString = Replace("*", "")
getCodeArray = Split(codeString, delimiter)

End Function

有没有办法可以设置正则表达式,这样我就可以直接将2个字母的子字符串作为mCol的匹配项返回,而不必使用replace()split()

1 个答案:

答案 0 :(得分:2)

最简单的是按s = "There is *AB**XY**DZ* more text" a = Split(s, "*") For i = 1 To UBound(a) Step 2 Debug.Print a(i) Next 拆分并得到结果的奇数元素:

*

其他选项可以尝试消极和积极的前瞻https://regex101.com/r/ZbEb4U/1 或匹配 let value = OperationDataHolder(data:[0x023F]) var parameter = 0x023F let data = NSData(bytes: &parameter, length: 2) self.sensorTagPeripheral.writeValue(data as Data, for: thisCharacteristic, type: CBCharacteristicWriteType.withResponse) s https://regex101.com/r/ZbEb4U/2