多行/表的正则表达式VBA?

时间:2016-06-06 08:27:44

标签: excel-vba expression vba excel

我有一个文本(如果需要,已存储在String变量中)。 该文本的结构如下:

(124314)GSK67SJ / 11 ADS SDK

等等等等等等等等等等 blah blah blah

blah blah blah

(298)2KEER / 98 EOR PRT

blah blah blah

等等等等等等等等等等

  • 单词之间的空格数是可变的;
  • 括号中的值是可变的,作为字母数字的长度 小组(这个小节总是以" /"然后是两个数字结束);
  • 文字" blah blah"最后可以划分为未知数 每行都有一个可变数量的字符
  • 最后两组字母各占3个字母。后 那些有" / n"紧接着,没有空格;
  • 列表下降0到N个元素。

对于他们每个人,我必须存储号码,前3个字母,第2个3个字母,以及"等等等等。在Excel文件的4列中。 让我们说列是A,B,C,D。结果应该如下(来自A1):

124314 | ADS | SDK |等等等等等等。

298 | EOR | PRT |等等......

.........

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

我设法解决了它

    Dim RegX As VBScript_RegExp_55.RegExp 'Rememeber to reference it...
Dim Mats As Object
Dim TextFiltered As String
Dim counter As Integer

Set RegX = New VBScript_RegExp_55.RegExp

With RegX
    .Global = True
    .MultiLine = True
    .Pattern = "[\s]{2,}(?!\(\s+(\d+)\s+\))" 'This will clear the annoying splitting into different lines of the "blah blah" A PART for the ones before "( number )"
    TextFiltered = .Replace(TextFiltered, " ") ' You could also write [\r\n] instead of [\s] but in that way you eliminate all the spaces in one hit
End With

With RegX 'This is the pattern you're looking for, the brackets mean different elements you could retrieve from the array of the results
    .Pattern = "\(\s+(\d+)\s+\)(\s+\w+/[0-9]{2}\s+)([A-Z]{3})\s+([A-Z]{3})\s+(.+)" 'I think you can remove the "+" from the "\s"
    Set Mats = .Execute(TextFiltered)
End With

For counter = 0 To Mats.Count - 1 'SubMatches is what will give you the various elements one by one (124314, ADS, SDK, etc)
    MsgBox Mats(counter).SubMatches(0) & " " & Mats(counter).SubMatches(2) & " " & Mats(counter).SubMatches(3) & " " & Mats(counter).SubMatches(4) 
Next