1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
Sub testREGEX()
Dim sChaine1 As String
Dim sChaine2 As String
Dim fResult1 As Boolean
Dim fResult2 As Boolean
Dim sPattern As String
' Suppression de la plage d'horaire, | compris
' Il ne doit rester que soit abcdef, soit abc
sChaine1 = "abc| 08:00 à 10:00 |def"
sChaine2 = "abc| 08:00 à 10:00"
sPattern = "..\d\d:\d\d à \d\d:\d\d.."
fResult1 = REGEX_Test(sPattern, sChaine1)
fResult2 = REGEX_Test(sPattern, sChaine2)
sChaine1 = REGEX_Replace(sPattern, sChaine1, "", False, True)
sChaine2 = REGEX_Replace(sPattern, sChaine2, "", False, True)
End Sub
' https://cafeine.developpez.com/access/tutoriel/regexp/
' https://www.developpez.net/forums/d2130886/logiciels/microsoft-office/excel/vba-regexp-pattern/#post11835975
' Pour utiliser Regex dans VBA, il faut dabord définir la référence dans léditeur VBE.
' Dans léditeur VBE, aller dans Outils > Références > Microsoft VBScript Regular Expressions
Function REGEX_Test(sPattern As String, sString As String) As Boolean
Dim oRegex As Object
Set oRegex = New RegExp
oRegex.Pattern = sPattern
REGEX_Test = oRegex.Test(sString)
End Function
Function REGEX_Replace(sPattern As String, sOldString As String, sNewString As String, fGlobal As Boolean, fIgnoreCase As Boolean) As String
Dim oRegex As Object
Set oRegex = New RegExp
oRegex.Pattern = sPattern
oRegex.Global = fGlobal
oRegex.IgnoreCase = fIgnoreCase
REGEX_Replace = oRegex.Replace(sOldString, sNewString)
End Function |
Partager