我正在尝试解决以下正则表达式:
https://regex101.com/r/pB0mD5/1
我想添加一个可选组来捕获每个匹配之间的文本段落(如果有的话)。
所以第一场比赛的最后一组是:
"[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
答案 0 :(得分:2)
可以使用此正则表达式
(?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
<强> RegexDemo 强>
注意强>
(?s) # It allows matching . with new lines (Alternative :- [\S\s])
(?= #Lookahead
\nNZZ #Match NZZ only if its followed by new line, otherwise it can match NZZ if its in middle of text
|
\z #End of string
)