Expressions régulières

Yule35240 - Modifié par Yule35240 le 19/08/2014 à 21:03
jordane45 Messages postés 38138 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 avril 2024 - 19 août 2014 à 21:44
Bonjour,

Je cherche comment récupérer l'IP contenue de le code source d'un Email.

Pour cela j'utilise le code suivant:

Option Explicit On

Public Class Form1

Private Sub Web_StatusTextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Web.StatusTextChanged
RichTextBox2.Clear()
RichTextBox2.Text = Web.DocumentText
Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim value1 As Integer = CInt(Int((255 * Rnd()) + 1))
Dim value2 As Integer = CInt(Int((255 * Rnd()) + 1))
Dim value3 As Integer = CInt(Int((255 * Rnd()) + 1))

Dim index As Integer = 0
While (index < RichTextBox2.Text.LastIndexOf("[" & value1 & "."))
RichTextBox2.Find("[" & value1 & ".", index, RichTextBox2.TextLength, RichTextBoxFinds.None)
RichTextBox2.SelectionBackColor = Color.LightCoral
RichTextBox2.SelectionColor = Color.White
index = RichTextBox2.Text.IndexOf("[" & value1 & ".", index) + 1
End While

Dim index1 As Integer = 0
While (index1 < RichTextBox2.Text.LastIndexOf("." & value2 & "."))
RichTextBox2.Find("." & value2 & ".", index1, RichTextBox2.TextLength, RichTextBoxFinds.None)
RichTextBox2.SelectionBackColor = Color.LightCoral
RichTextBox2.SelectionColor = Color.White
index1 = RichTextBox2.Text.IndexOf("." & value2 & ".", index1) + 1
End While

Dim index2 As Integer = 0
While (index2 < RichTextBox2.Text.LastIndexOf("." & value3 & "]"))
RichTextBox2.Find("." & value3 & "]", index2, RichTextBox2.TextLength, RichTextBoxFinds.None)
RichTextBox2.SelectionBackColor = Color.LightCoral
RichTextBox2.SelectionColor = Color.White
index2 = RichTextBox2.Text.IndexOf("." & value3 & "]", index2) + 1
End While
End Sub

End Class



Mon code n'est pas propre du tout, comment peut on faire à l'aide des expressions régulières pour obtenir le même résultat?

Merci d'avance....

2 réponses

jordane45 Messages postés 38138 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 avril 2024 344
19 août 2014 à 21:12
Visual Basic... ok.. mais ... VB6 ou VB.net ?? (je pense que c'est du .net.. mais bon.. je vais attendre ta réponse pour déplacer le sujet au BON ENDROIT )
0
jordane45 Messages postés 38138 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 avril 2024 344
Modifié par jordane45 le 19/08/2014 à 21:19
Enfin bon...

Si c'est bien en VB.NET :



Voici ce que tu aurais pu trouver ICI :VB.NET+regex


VB.NET Code Example:
Imports System.Text.RegularExpressions
Module Module1
  Sub Main()
    Dim sourcestring as String = "replace with your source string"
    Dim re As Regex = New Regex("(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)",RegexOptions.Multiline)
    Dim mc as MatchCollection = re.Matches(sourcestring)
    Dim mIdx as Integer = 0
    For each m as Match in mc
      For groupIdx As Integer = 0 To m.Groups.Count - 1
        Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames(groupIdx), m.Groups(groupIdx).Value)
      Next
      mIdx=mIdx+1
    Next
  End Sub
End Module


Ce qui te donne comme résultats :

pour le texte suivant :

qdsfqs
fqdsf
qdsfqds
gfnpùmklmù poefdmj fqdsfqdsfg qd s165fjh.
246
3540.4465.454

fgfdsgfdsgpm qdfj

192.168.1.10

qdsfqfqs qdsf dsfpqsdiojgf
sgqjkfdsgfgh


$matches Array:
(
[0] => Array
(
[0] => 192.168.1.10
)

[1] => Array
(
[0] => 192
)

[2] => Array
(
[0] => 168
)

[3] => Array
(
[0] => 1
)

[4] => Array
(
[0] => 10
)

)


Avant de poser une question, merci de lire la charte du site.
Cordialement, Jordane
0
Bonsoir Jordane, tout d'abord merci de m'avoir répondu!

J'utilise visual basic 2010
0
jordane45 Messages postés 38138 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 avril 2024 344
19 août 2014 à 21:44
Message déplacé en vb.net.
0
Rejoignez-nous