Regex 正则表达式-在最后一组后面包含段落

Regex 正则表达式-在最后一组后面包含段落,regex,vba,Regex,Vba,我正在尝试求解以下正则表达式: Sub TestExp() Dim sTest As String Dim sExpression As String sTest = Range("A1").Value sExpression = "(?s)(NZZ[A-Z\d\_\-]*)\s([A-Z\(\) ]*)\s(SECTOR|FIR-P|FIR)\s([0-9]*)*\s*(FT|FL)?(.*?)(?=\n\bNZZ|\z)" Call ReadExp

我正在尝试求解以下正则表达式:

Sub TestExp()
    Dim sTest As String
    Dim sExpression As String

    sTest = Range("A1").Value 
    sExpression = "(?s)(NZZ[A-Z\d\_\-]*)\s([A-Z\(\) ]*)\s(SECTOR|FIR-P|FIR)\s([0-9]*)*\s*(FT|FL)?(.*?)(?=\n\bNZZ|\z)"
    Call ReadExpression(sResult, sExpression)
End Sub
Sub ReadExpression(strData As String, sExpression As String)
    Dim myRegExp As RegExp
    Dim myMatches As MatchCollection
    Dim myMatch As Match

    Set myRegExp = New RegExp
    myRegExp.Global = True
    myRegExp.MultiLine = True
    myRegExp.Pattern = sExpression

    Set myMatches = myRegExp.Execute(strData)

    Dim i As Integer
    Dim ii As Integer
    Dim strMatch As String

    i = shtOutput.Range("A" & Rows.Count).End(xlUp).row

    For Each myMatch In myMatches
        i = i + 1
        For ii = 1 To myMatch.SubMatches.Count
          shtOutput.Cells(i, ii).Value = myMatch.SubMatches(ii - 1)
        Next
        DoEvents
    Next

End Sub

我想添加一个可选组来捕获每个匹配之间的文本段落(如果有)

所以第一场比赛的最后一组是:

"[General:] RVSM exclusive airspace FL290 to FL410, using the single alternate flight level allocation system described in Annex 2, Appendix 3a, except track allocation shall be 270 to 089 degrees (north), in lieu of 000 to 179 degrees (east), and shall be 090 to 269 degrees (south), in lieu of 180 to 359 degrees (west). Transponder mandatory - all controlled airspace (CTR and CTA) within the NZ FIR."
[General:] RNP airspace FL245 to FL600. RVSM airspace FL290 to FL410 Transponder mandatory - all oceanic control areas (OCA) within the Auckland Oceanic FIR.
第二场比赛的最后一组是:

"[General:] RVSM exclusive airspace FL290 to FL410, using the single alternate flight level allocation system described in Annex 2, Appendix 3a, except track allocation shall be 270 to 089 degrees (north), in lieu of 000 to 179 degrees (east), and shall be 090 to 269 degrees (south), in lieu of 180 to 359 degrees (west). Transponder mandatory - all controlled airspace (CTR and CTA) within the NZ FIR."
[General:] RNP airspace FL245 to FL600. RVSM airspace FL290 to FL410 Transponder mandatory - all oceanic control areas (OCA) within the Auckland Oceanic FIR.
用于测试表达式的VBA代码:

Sub TestExp()
    Dim sTest As String
    Dim sExpression As String

    sTest = Range("A1").Value 
    sExpression = "(?s)(NZZ[A-Z\d\_\-]*)\s([A-Z\(\) ]*)\s(SECTOR|FIR-P|FIR)\s([0-9]*)*\s*(FT|FL)?(.*?)(?=\n\bNZZ|\z)"
    Call ReadExpression(sResult, sExpression)
End Sub
Sub ReadExpression(strData As String, sExpression As String)
    Dim myRegExp As RegExp
    Dim myMatches As MatchCollection
    Dim myMatch As Match

    Set myRegExp = New RegExp
    myRegExp.Global = True
    myRegExp.MultiLine = True
    myRegExp.Pattern = sExpression

    Set myMatches = myRegExp.Execute(strData)

    Dim i As Integer
    Dim ii As Integer
    Dim strMatch As String

    i = shtOutput.Range("A" & Rows.Count).End(xlUp).row

    For Each myMatch In myMatches
        i = i + 1
        For ii = 1 To myMatch.SubMatches.Count
          shtOutput.Cells(i, ii).Value = myMatch.SubMatches(ii - 1)
        Next
        DoEvents
    Next

End Sub
可以使用这个正则表达式

(?s)(NZZ[A-Z\d\_\-]*)\s([A-Z\(\) ]*)\s(SECTOR|FIR-P|FIR)\s([0-9]*)*\s*(FT|FL)?(.*?)(?=\n\bNZZ|\z)
        <--------->     <-------->
     can be written as   No need
           [\w-]*      to escape ()
                    in character class

如果您使用的是一种语言,那么还可以编写一个解析器来检查每一行。这可能比一个复杂的正则表达式更容易维护。@rock321987你能发布一个完美的答案吗?您好,对不起,我可能有点仓促,我也在尝试在excel vba中使用它。MS VbScript正则表达式5.5。执行失败,没有特定的错误消息。有什么想法吗?@Reafidy你能在ideone或任何其他网站上分享代码吗?我已经更新了原始问题,加入了我用来测试表达式的vba代码。这就是你需要的吗?@Reafidy你可以试试
(?s)(NZZ[A-Z\d\\\-]*)\s+([A-Z\(\)]*)\s+(扇区FIR-P | FIR)\s+([0-9]*)*\s*(FT | FL)?(*?)(?=\nNZZ |\Z)
regex。。同时,我认为它与捕捉群体有关。它不喜欢表达式开头的(?s)。