Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 49962

Multiple condition in regex

$
0
0
I want to part of full name with Bachelor academic
For example in col b
Dr.ir. Alex,Msc col C dr.ir col d =Alex col e = msc
In my code has sucsess if any sign after bachelor to col c,d,e
Ir.alex or ir alex
Or ir. Alex
But my problem
If name follwed by bachelor in back of name
H.alex,Ir is sucses if after name is (,)
But failed
If Ir.Alex wiliam Msc
Or ir.Alex wiliam Phd
Result col c ir col d = name and col e
Phd

Code:


makro nya
Sub test()
Dim ar(), i&, ii&, a, temp As String, RE As Object: Set RE = CreateObject("vbscript.Regexp")
a = Columns(2).SpecialCells(2).Value: Columns("c:f").ClearContents
ReDim ar(1 To UBound(a) + 1, 1 To 4)
 
RE.Global = True: RE.ignoreCase = True: RE.MultiLine = True
RE.Pattern = "(.*?(?=,)),?(.*)" 'regex ke 1 split koma pertama jumpa alias titel belakang
    For i = 2 To UBound(a)
      If RE.test(a(i, 1)) Then
        ar(i - 1, 4) = RE.Replace(a(i, 1), "$1") 'nama
        ar(i - 1, 3) = RE.Replace(a(i, 1), "$2") 'koma kebelakang
      Else
          ar(i - 1, 4) = a(i, 1)
      End If
    Next i
        'regex ke 2 untuk ambil nama titel depan atau tanpa titel
        RE.Pattern = "(.*\b(ir|prof|hj|h|d(rs|rg|r|ra|rh))\b)?(?:\.)?(.+)"
    For i = 1 To UBound(ar)
          ar(i, 1) = RE.Replace(ar(i, 4), "$1") 'titel depan
          ar(i, 2) = RE.Replace(ar(i, 4), "$4") 'nama
    Next i
[c1].Resize(, 3).Value = Array("Nama", "Titel Depan", "Titel Belakang")
[c2].Resize(UBound(ar, 1), UBound(ar, 2) - 1).Value = ar
End Sub


Viewing all articles
Browse latest Browse all 49962

Trending Articles