VBA - wyrażenia regularne

VBA - wyrażenia regularne

Ten samouczek ma na celu wyjaśnienie wszystkich zawiłości związanych z używaniem wyrażeń regularnych. Dowiesz się, jak poruszać się po łańcuchach w celu znalezienia podciągów używających wyrażeń regularnych (powszechnie nazywanych RegExp ).

Wymagania wstępne

W edytorze VBA.
  • Przejdź do menu Narzędzia> Odniesienia.
  • Przejrzyj różne odniesienia i sprawdź następującą opcję „Microsoft VBScript Regular Expression 5.5”.

Składnia i deklaracja

W tych przykładach jedynymi wymaganymi warunkami są:

Wyrok

Dostępne są dwie opcje:

Jako obiekt

 Dim reg As Object Set reg = CreateObject ("vbscript.regexp") 

VBScript_RegExp_55.regexp

 Dim reg As VBScript_RegExp_55.regexp Set reg = Nowy VBScript_RegExp_55.regexp 

Właściwości

Bez względu na to, jak określiłeś RegExp, właściwości będą miały taką samą składnię.

Wzór

Wzór musi być zgodny z podciągiem. Umożliwia nawigację po łańcuchu.

 reg.Pattern = "IciLeMotif" 

Test

Przetestujemy łańcuch, jeśli wzorzec zostanie znaleziony, zwróci następującą wartość boolowską: True.

 reg.Test = "IciLeMotif" 

Ciąg zaczyna się od A (wielkie litery)

Wzór będzie:
  • na początku ciągu
  • jest A

Zapis tego wzorca jest następujący: ^ A

 Funkcja Prem_Lettre_A (wyrażenie As String) Jako Boolean Dim reg Jako obiekt Set reg = CreateObject ("vbscript.regexp") 'Le Pattern to le motif que l'on recherche' début de chaîne: ^ 'doit être: A reg.Pattern = „^ A” 'le test renvoie un Boolean (parfait pour notre fonction Booléenne !!!) Prem_Lettre_A = reg.test (wyrażenie) End Function 

Aby zadzwonić do funkcji:

 Sub Test_A () 'Nous allons chercher si un String rozpocznij par "A" Majuscule MsgBox Prem_Lettre_A ("alors?") MsgBox Prem_Lettre_A ("Ahhh") MsgBox Prem_Lettre_A ("Pasal non?") End Sub 

Ciąg zaczyna się od a lub A

Zapis wzoru to [aA]

 Sub Test_a_ou_A () 'Nous allons chercher si un String rozpocznij par "a" ou "A" MsgBox Prem_Lettre_a_ou_A ("alors?") MsgBox Prem_Lettre_a_ou_A ("Ahhh") MsgBox Prem_Lettre_a_ou_A ("Pas non non?") End Sub Function Prem_Lettre_a_ou_A ( wyrażenie As String) Jako Boolean Dim reg Jako obiekt Set reg = CreateObject ("vbscript.regexp") 'ici la première lettre: ^' doit être: a ou A => [aA] reg.Pattern = "^ [aA]" Prem_Lettre_a_ou_A = reg.test (wyrażenie) End Function 

Ciąg rozpoczyna się wielką literą

Zapis tego wzorca to: [AZ]

 Sub Commence_par_Majuscule () MsgBox "alors? Commod par une majuscule:" & Prem_Lettre_Majuscule ("alors?") MsgBox "Ahhh rozpocznij par une majuscule:" & Prem_Lettre_Majuscule ("Ahhh") MsgBox "Pas mal non? Comm une majuscule:" & Prem_Lettre_Majuscule ("Pas non non?") End Sub Function Prem_Lettre_Majuscule (wyrażenie As String) Jako Boolean Dim reg Jako obiekt Set reg = CreateObject ("vbscript.regexp") 'ici la première lettre: ^' doit être une lettre: [AZ] reg.Pattern = "^ [AZ]" Prem_Lettre_Majuscule = test reg.test (wyrażenie) Funkcja końcowa 

Ciąg kończy się

Użyj następującej notacji: $

 Sub Fini_Par () MsgBox "La wyrażenie: Les RegExp c'est super! Se termine par super:" & Fin_De_Phrase ("Les RegExp c'est super!") MsgBox "La wyrażenie: C'est super les RegExp! Se termine par super: "& Fin_De_Phrase (" C'est super les RegExp! ") End Sub Function Fin_De_Phrase (wyrażenie jako String) Jako Boolean Dim reg Jako obiekt Set reg = CreateObject (" vbscript.regexp ") 'La fin de la chaine doit être : Wspaniały! 'notation de fin de chaîne: $ reg.Pattern = "super! $"' note le $ se miejsce à la fin ... Fin_De_Phrase = reg.test (wyrażenie) End Function 

Ciąg zawiera liczbę

Notacja dla tego wzorca to [0-9] . Jeśli chcesz znaleźć numer od 3 do 7: [3-7]

 Sub Contient_un_chiffre () MsgBox "aze1rty contient un chiffre:" & A_Un_Chiffre ("aze1rty") MsgBox "azerty contient un chiffre:" & A_Un_Chiffre ("azerty") End Sub Function A_Un_Chiffre (wyrażenie jako String) Jako Boolean Dim reg As Object Set reg = CreateObject ("vbscript.regexp") 'doit comporter un chiffre de 0 à 9 n'importe ou (début, milieu, fin de chaine ...) reg.Pattern = "[0-9]"' remarque [0 -9] s'écrit également: d 'reg.Pattern = "d" A_Un_Chiffre = reg.test (wyrażenie) End Function 

Ciąg zawiera 3-cyfrowy numer

Będziemy teraz „kwantyfikować” w naszym RegExp. Aby znaleźć trzy wystąpienia, użyjemy nawiasów klamrowych {}.

 Sub Contient_Un_Nombre_A_trois_Chiffres () MsgBox "aze1rty contient 3 chiffres:" & Nb_A_Trois_Chiffre ("aze1rty") MsgBox "a1ze2rty3 contient 3 chiffres:" & Nb_A_Trois_Chiffre ("a1ze2rty3") MsgBox "azer123ty contient 3 chiffres:" & Nb_A_Trois_Chiffre ("azer123ty") Podfunkcja Nb_A_Trois_Chiffre (wyrażenie As String) Jako Boolean Dim reg Jako obiekt Set reg = CreateObject ("vbscript.regexp") 'doit comporter 3 chiffres de 0 à 9 qui se suivent' le nombre d'occurrence se note {} reg.Pattern = "d {3}" 'équivalant de: reg.Pattern = "[0-9] {3}" Nb_A_Trois_Chiffre = test reg.test (wyrażenie) Funkcja końcowa 

Ciąg zawiera 3 oddzielne liczby

Kod

 Sub Contient_trois_Chiffres () MsgBox "aze1rty contient 3 chiffres séparés:" & Trois_Chiffre ("aze1rty") MsgBox "a1ze2rty3 contient 3 chiffres séparés:" & Trois_Chiffre ("a1ze2rty3") MsgBox "azer123ty contient 3 chiffres séparés:" & Trois_Chiffre ("azer123ty ") End Sub Function Trois_Chiffre (wyrażenie As String) Jako Boolean Dim reg As Object Set reg = CreateObject (" vbscript.regexp ") 'doit comporter 3 chiffres de 0 à 9 qui ne se suivent pas' le nombre d'occurrence se note {} 'le point (.) indique n'import quel caractère sauf le saut de ligne' le + indique que ce qui le précède (ici le point) doit être représenté une oune in infinité de fois reg.Pattern = "(.) + (d {1}) (.) + (d {1}) (.) + (d {1}) "Trois_Chiffre = reg.test (wyrażenie) End Function 

Wariant

 Pod Contient_trois_Chiffres_Variante () MsgBox "aze1rty contient 3 chiffres séparés" i Trois_Chiffre_Simplifiee ( "aze1rty") MsgBox "a1ze2rty3 contient 3 chiffres séparés" i Trois_Chiffre_Simplifiee ( "a1ze2rty3") MsgBox "azer123ty contient 3 chiffres séparés" i Trois_Chiffre_Simplifiee ( "azer123ty ") End Sub Function Trois_Chiffre_Simplifiee (wyrażenie As String) Jako Boolean Dim reg Jako zestaw obiektów reg = CreateObject (" vbscript.regexp ") 'Comme le même motyw: (.) ​​+ (D {1}) se répète trois fois: reg.Pattern = "(. + d {1}) {3}" Trois_Chiffre_Simplifiee = reg.test (wyrażenie) End Function 

Przykład użycia

W naszym przykładzie celem jest określenie, czy ciąg składa się z następującej sekwencji:
  • 1: Ciąg rozpoczyna się słowem „Vis”
  • 2: następuje spacja
  • 3: a następnie od 1 do 3 liter
  • 4: następuje spacja
  • 5: po której następuje litera M
  • 6: a następnie od 1 do 2 liter
  • 7: następuje myślnik
  • 8: a następnie od 1 do 3 liter
  • 9: następuje słowo „classe” otoczone spacjami AND małymi literami
  • 10: kontynuacja 1-2 liter
  • 11: następuje kropka
  • 12: po którym następuje litera

Zauważ, że : Każdy blok wzoru jest zawarty między nawiasami.

Notacja:

  • 1- => (^ Vis)
  • 2- => ()
  • 3- => ([a-zA-Z] {1, 3})
  • 4- => () --- Uwaga: na aurait pu l'insérer avec le M qui suit ... Mis pour l'exemple
  • 5- => (M)
  • 6- => ([a-zA-Z] {1, 2})
  • 7- => (-)
  • 8- => ([a-zA-Z] {1, 3})
  • 9- => (classe)
  • 10- => ([a-zA-Z] {1, 2})
  • 11- => (.)
  • 12- => ([a-zA-Z] {1})

Daje nam to następujący wzór

„(^ Vis) () ([a-zA-Z] {1, 3}) () (M) ([a-zA-Z] {1, 2}) (-) ([a-zA-Z ] {1, 3}) (classe) ([a-zA-Z] {1, 2}) (.) ([A-zA-Z] {1}) ”

Kod:

 Sub Main () If VerifieMaChaine ("Vis xx Mxx-x classe xx.x") Następnie MsgBox "good" Else MsgBox "pas glop" End If xx.x ") Następnie MsgBox" good "Else MsgBox" pas glop "End If End Sub Function VerifieMaChaine (wyrażenie As String) Jako Boolean Dim reg Jako obiekt Set reg = CreateObject (" vbscript.regexp ") ' ale ten jest najbardziej kompletny reg.Pattern = "(^ Vis) () ([a-zA-Z] {1, 3}) () (M) ([a-zA-Z] {1, 2} ) (-) ([a-zA-Z] {1, 3}) (classe) ([a-zA-Z] {1, 2}) (.) ([a-zA-Z] {1}) „VerifieMaChaine = reg.test (wyrażenie) End Function End Sub 

Ściągnij

Kody opisane w tym samouczku są wymienione w tym skoroszycie: //cjoint.com/14au/DHgoqY7wwIw.htm
Poprzedni Artykuł Następny Artykuł

Najważniejsze Wskazówki