Récupération du message (regex.replace).

Résolu
NeriXs Messages postés 258 Date d'inscription lundi 4 mai 2015 Statut Membre Dernière intervention 27 février 2024 - 21 déc. 2017 à 08:29
NeriXs Messages postés 258 Date d'inscription lundi 4 mai 2015 Statut Membre Dernière intervention 27 février 2024 - 21 déc. 2017 à 15:45
Bonjour,
J'essaie de récupérer le résultat de mon remplacement dans mon message de sortie afin d'obtenir ceci:
Const texte:
\-\\\*-****/Un test, Pourquoi faire? c'est fou ça! \****-*///*/
Result:
\(-)\\\(*)(-)****(/)Un( )test(,)( )Pourquoi( )faire(?)( )c(')est( )fou( )ça(!)( )\****(-)(*)///(*)(/)
Pouvez-vous m'aider?

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

arrDicoItem = objDico.Items

bpermute = True
Do While bpermute = True
bpermute = False
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

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

'result = regex.Replace ligne 65
MsgBox texte & vbCrLf & "result" & vbCrLf & strList,,"Liste"
'MsgBox texte & vbCrLf & result & vbCrLf & 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

'#######################################################
result = regex.Replace(texte, "($2)")
MsgBox result
'#######################################################

Set matches = Nothing
Set regex = Nothing
End Sub
A voir également:

1 réponse

cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
21 déc. 2017 à 14:37
Bonjour NeriXs,

Il te suffit de lire le tableau à l'envers.
Dim result
result = texte
For i=UBound(arrDicoItem) To 0 Step -1
   Dim parse1, parse2, intstr
   parse1 = "(" & Left(Split(arrDicoItem(i),"trouvée " & """")(1),1) & ")"
   parse2 = Split(arrDicoItem(i),"position: ")(1)
   intstr = Len(result)
   result = Left(result,parse2) & parse1 & Right(result, intstr - parse2 - 1)
Next

MsgBox result



jean-marc
1
NeriXs Messages postés 258 Date d'inscription lundi 4 mai 2015 Statut Membre Dernière intervention 27 février 2024 1
21 déc. 2017 à 15:45
Bien vu cs_JMO Merci à toi!
0
Rejoignez-nous