Contrôle de 2 variables avec RegEx

Résolu
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 - 11 nov. 2016 à 19:52
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 - 11 nov. 2016 à 20:58
Bonsoir le forum,
Je souhaite contrôler le contenu de deux variables.
Si var1 ou var2 contiennent ";" et/ou ":" alors True.
Dans mes tests avec Regex, j'ai contatené var1 et var2,
est-ce la bonne méthode ???
        Dim testPlanningGlobal As String = "L-VD 0-24"
        Dim testPlanningService As String = "L-V 7:30-19"

        Dim testPlannings As String = testPlanningGlobal & testPlanningService

        If returnMatchPlanning(testPlannings) = True Then
            MessageBox.Show(testPlanningGlobal & Environment.NewLine & testPlanningService)
        End If

    Function returnMatchPlanning(ByVal word As String) As Boolean
        Dim patern As String = "[:;]"
        For Each m As Match In Regex.Matches(word, patern)
            If (m.Success) Then
                Return True
            End If
        Next
        Return False
    End Function

Merci de vos suggestions.
jean-marc
A voir également:

2 réponses

NHenry Messages postés 15112 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 13 avril 2024 159
11 nov. 2016 à 20:17
Si c'est juste pour vérifier si une chaine contient l'un et/ou l'autre, il y a plus simple et surtout moins compliqué/lourd qu'un regex :
Dim lToTest=";:"
For Each lChar in lToTest
    If MaChaine.Contains(lChar) Then Return True;
Next
Return False

(Code tapé rapidement donc peut être à adapter)
0
Whismeril Messages postés 19023 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 656
11 nov. 2016 à 20:28
Bonsoir,

même un poil plus court
Return texte.Contains(":") Or texte.Contains(";")
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
11 nov. 2016 à 20:58
Merci à NHenry et Whismeril de vos propositions,

J'en conclus que le concaténation des deux variables est préférable au lieu d'utiliser if var1 ... = True And var2 ... = True;

        Dim testPlanningGlobal As String = "L-VD 0-24"
        Dim testPlanningService As String = "L-V 73019"

        Dim testPlannings As String = testPlanningGlobal & testPlanningService

        If (testPlannings.Contains(":") Or testPlannings.Contains(";")) = True Then
            MessageBox.Show("contient  : ou ;")
        Else
            MessageBox.Show("ne contient ni : ni ;")
        End If


Je valide donc vos réponses.

Bonne soirée à vous.

jean-marc
0
Rejoignez-nous