RegExp Récupérer référence groupes de capture.
Bonjour,
Ce code me permet de chercher si le TxtSource contient l’un des caractères contenue dans le pattern.
Pour le remplacement je dois spécifier la position de mes groupes de capture manuellement "$7$12".
J’aimerais récupérer ces groupes de capture automatiquement.
J’ai essayé de jouer un peu avec une boucle sur les caractères de mon pattern et un appel match.Submatches(i), mais je ne m’en sort pas.
Un petit coup de main SVP ?
Code:
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
| TxtSource = "Un test? Pourquoi faire? c'est fou ça?"
ReslutSpeciChars = ""
Dim strText, RegularExpressioN
FindSpeciChars = "(\\)|(\/)|(\*)|(\+)|(\-)|(\=)|(\?)|(\!)|(\:)|(\;)|(\,)|(\')|(\&)|(\~)|(\{)|(\})|(\()|(\))|(\[)|(\])|(\@)|(\^)|(\¨)|(\$)|(\£)|(\)|(\.)|(\<)|(\>)|(\|)"
strText = TxtSource
Set RegularExpressioN = New RegExp
RegularExpressioN.Pattern = FindSpeciChars
RegularExpressioN.Global = True
Set Matches = RegularExpressioN.Execute(strText)
If Matches.Count > 0 Then
MatchMsg = Matches.Count & " correspondance(s) trouvée(s)." & vbCRLF
For Each Match In Matches
MatchMsg = MatchMsg & "Correspondance trouvée """ & Match.Value & """ en position: " & Match.FirstIndex & vbCrLf
Next
'MsgBox MatchMsg
Else
MsgBox "Aucun match", 0, "VBScript RegExp Tester"
End If
strText = RegularExpressioN.Replace(strText, "$7$12")
Set RegularExpressioN = Nothing
ReslutSpeciChars = strText
MsgBox TxtSource & vbCrLf & ReslutSpeciChars & vbCrLf & MatchMsg |