Boucle VBA en VBScript

Résolu
NeriXs Messages postés 258 Date d'inscription lundi 4 mai 2015 Statut Membre Dernière intervention 27 février 2024 - Modifié le 25 nov. 2019 à 11:25
 jojo - 24 nov. 2019 à 18:48
Bonjour,
Pourriez-vous m'aider à porter cette boucle VBA en Vbscript SVP?

 For Each caractere In Array("1", "2", "3")
      Recherche caractere, texte
 Next


Pour info Recherche fait référence a une sub "Sub Recherche"

texte = "Poste1, Poste2 et Poste3"

23 réponses

NeriXs Messages postés 258 Date d'inscription lundi 4 mai 2015 Statut Membre Dernière intervention 27 février 2024 1
20 déc. 2017 à 20:57
Tout Caractère doit être précédé de \ pour que le pattern fonction en théorie même l'espace.
c'est OK avec "\:"

Const texte = "\-\\\*-****/Un test, Pourquoi faire? c'est fou: ça! \****-*///*/"

Dim objDico
Dim myarray, arrDicoItem
Dim item, strList, cprovisoire
Dim i, t, bpermute

myarray = Array("\?", "\ ", "\,", "\*", "\!", "\\", "\-", "\:", "\'")
Set objDico = CreateObject("Scripting.Dictionary")

For Each item In myarray
Recherche(item)
Next

' Tri
arrDicoItem = objDico.Items

bpermute = True
Do While bpermute = True 'Il faut au moins parcourir une fois
bpermute = False 'On tourne tant que l'on bouge des valeurs
For t = 0 To UBound(arrDicoItem) -1
If CInt(Split(arrDicoItem(t),": ")(1)) > CInt(Split(arrDicoItem(t + 1),": ")(1)) Then
cprovisoire = arrDicoItem(t)
arrDicoItem(t) = arrDicoItem(t + 1)
arrDicoItem(t + 1) = cprovisoire
bpermute = True
End If
Next
Loop
'Affichage du résultat classé
strList = objDico.Count & " correspondance(s) trouvée(s):" & vbcrlf
For i = 0 To UBound(arrDicoItem)
strList = strList & "(" & i+1 & ")" & " " & arrDicoItem(i) &vbCrLf
Next
Set objDico = Nothing

MsgBox strList,,"Liste"

WScript.Quit


Sub Recherche(Caractere)
Dim regex, matches
Dim Pattern, match, i
Pattern = "([^" & Caractere & "]|^)(" & Caractere & ")(?!" & Caractere & ")"

Set regex = New RegExp
regex.Pattern = Pattern
regex.Global = True

Set matches = regex.Execute(texte)

For Each match In matches
For i = 1 To match.Submatches.Count - 1 Step 2
If Not (match.Submatches(i) = "") Then
objDico.Add match.FirstIndex + Len(match.Value) - 1, _
"Correspondance trouvée """ & _
match.Submatches(i) & """ en position: " & _
match.FirstIndex + Len(match.Value) - 1
End If
Next
Next
Set matches = Nothing
Set regex = Nothing
End Sub
0
NeriXs Messages postés 258 Date d'inscription lundi 4 mai 2015 Statut Membre Dernière intervention 27 février 2024 1
20 déc. 2017 à 21:03
Pourrais tu m'expliquer le -1 de la ligne 21?

 For t = 0 To UBound(arrDicoItem) -1 
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
20 déc. 2017 à 21:26
La fonction UBound renvoie le plus grand indice disponible pour la dimension indiquée d'un tableau.
Souvent on utilise ubound(myarray)-1 car la dernière dimension est un vbcrlf (Combinaison de retour chariot et de saut de ligne).
Regarde sur Google pour télécharger la bible du vbs (SCRIPT56.CHM).
0
NeriXs Messages postés 258 Date d'inscription lundi 4 mai 2015 Statut Membre Dernière intervention 27 février 2024 1
20 déc. 2017 à 21:32
OK, merci pour le coup de main!
0
Rejoignez-nous