我正在嘗試為以下文本創建模式
not included
4680145876
some text some text ffgg
30905102511638
1
other text other text
no included
這是我的嘗試
^\s*\d{6,10}(?:\n(?!\s*\d{1,}\n).*){5}
我將在 VBA 中使用這樣的模式 預期的輸出要突出顯示(五行)
468049876
some text some text ffgg
30905102639638
1
other text other text
** 我在遇到問題時更新了問題假設文本是這樣的
not included
468041476
some text some text ffgg
31605102764638
1
other text other text
extra line
416524332
some text some text ffgg
30905103594638
1
other text other text
extra line
6354422
no included
在這里,我需要塊來遵循以下順序: 1- 6 到 12 位數字 2- 然后一行中的一些文本 3- 數字等于 14 位 4- 1 到 3 位數字 5- 文本(這是問題此文本可能在兩行而不是一行中),我需要將該額外的行作為一行包括在內,以便文本示例的輸出
468049876
some text some text ffgg
30905103685638
1
other text other text extra line
和
416524332
some text some text ffgg
30905101497638
1
other text other text extra line
我的意思是文本將只包含兩個塊(每行五行)
我正在使用這樣的代碼:
With CreateObject("VBScript.RegExp") .Global = True: .MultiLine = True: .IgnoreCase = True .Pattern = sPattern If .Test(sInput) Then Set col = .Execute(sInput) For i = 0 To col.Count - 1 x = Split(col.Item(i), vbLf) cnt = cnt 1 For j = LBound(x) To UBound(x) a(i 1, j 1) = Application.WorksheetFunction.Clean(Trim(x(j))) Next j Next i End If結束于
現在,在遍歷匹配項時,我得到了超過五個專案的變數 x。我預計只能得到五個專案。如何分別拿起每場比賽的第二組?
uj5u.com熱心網友回復:
在我看來,您應該在否定條件下檢查 6-10 位數字,并匹配空格位元組換行符,您可以使用[^\S\r\n]:
^( *\d{6,12} *\n.*\n *\d{14} *\n *\d{1,3} *)((?:\n(?! *\d{6,10} *$). )*)
請參閱正則運算式演示。詳情:
^- 一行的開始(記得使用)(- 第 1 組開始:*- 零個或多個空格\d{6,10}- 六到十位數字*- 零個或多個空格\n.*- 一條線\n *\d{14} *- 一行由零個或多個空格括起來的 14 位數字\n *\d{1,3} *- 一行一到三位數,中間有零個或多個空格
)- 第 1 組結束((?:\n(?! *\d{6,10} *$). )*)- 第 2 組:(?:- 非捕獲組的開始:\n- LF 行結束(?! *\d{6,12} *$)- 不緊跟零個或多個空格、六到十二個數字、零個或多個空格和行尾.- 非空行(除換行符以外的一個或多個字符盡可能多)
)*- 分組結束,出現零次或多次。
)- 第 2 組結束。
獲得匹配后,第 2 組包含最后一行行,因此您可以隨心所欲地操作該文本,然后與第 1 組值連接。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/353781.html
