Création RegEx après download url

Résolu
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 - 21 juin 2018 à 09:57
Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 - 24 juin 2018 à 21:49
Bonjour le Forum,

Après download d'une url, j'obtiens
<body bgcolor='#5A92AF'><table border=2 width='100%'><tr><th>Environnement de production</th></tr></table><table border='1' cellspacing='2' align='left' style='font-size:14; font-family: arial;'>
<td style='width: 148px; background-color: rgb(255,255,0)'; ><a href='menu.php <view-source:http://serveur_cible/menu.php>' >menu </td></table><br /><div align='center' style='font-size:16; font-family: arial;'><H2>Commandes </H2></div><table border='1' cellspacing='2' align='center' style='font-size:14; font-family: arial;'>
<td style='width: 248px; background-color: rgb(255,255,0)'; ><a href='list_command.php?tri=1 <view-source:http://serveur_cible/list_command.php?tri=1>'>command_id</td><td style='width: 248px; background-color: rgb(255,255,0)'; >nombre services</td><td style='width: 248px; background-color: rgb(255,255,0)'; ><a href='list_command.php?tri=0 <view-source:http://serveur_cible/list_command.php?tri=0>'>name</td><td style='width: 248px; background-color: rgb(255,255,0)'; ><a href='list_command.php?tri=2 <view-source:http://serveur_cible/list_command.php?tri=2>'>line</td><td style='width: 248px; background-color: rgb(255,255,0)'; >example</td><td style='width: 248px; background-color: rgb(255,255,0)'; >cmd_cat_id</td><td style='width: 248px; background-color: rgb(255,255,0)'; ><a href='list_command.php?tri=3 <view-source:http://serveur_cible/list_command.php?tri=3>'>type</td></tr>

<td><b>321</b></td><td><b><a href='list_command_service.php?id=321&tri=1 <view-source:http://serveur_cible/list_command_service.php?id=321&tri=1>'>0</b></td><td><b>A-CHK-PMU-ANALYSLOG</b></td><td><b>$USER1$/wrap.pl '$HOSTNAME$' '$SERVICEDESC$' '$HOSTDOWNTIME$' '$SERVICEDOWNTIME$' $USER1$/check_nrpe -t 500 -n -H $HOSTNAME$ -p 5666 -c $SERVICEDESC$ -a analyseLog$SERVICEDESC$.ini</b></td><td><b></b></td><td><b></b></td><td><b>2</b></td></td>
</tr>
<td><b>322</b></td><td><b><a href='list_command_service.php?id=322&tri=1 <view-source:http://serveur_cible/list_command_service.php?id=322&tri=1>'>1234</b></td><td><b>A-CHK-PMU-ORACLE-CNX</b></td><td><b>$USER1$/wrap.pl '$HOSTNAME$' '$SERVICEDESC$' '$HOSTDOWNTIME$' '$SERVICEDOWNTIME$' $USER1$/check_nrpe -t 120 -n -H $HOSTNAME$ -p $_HOSTNRPEPORT$ -c oracle -a $ARG1$ $ARG2$ $ARG3$ $ARG4$ $ARG5$ $ARG6$ $ARG7$ $ARG8$ $ARG9$ $ARG10$</b></td><td><b>!$_SERVICEUSER$!$_SERVICEPWD$!$_SERVICESID$!$_SERVICEIP!$_SERVICEPORT$!$_SERVICEORABASE$!$_SERVICEORAHOME$</b></td><td><b></b></td><td><b>2</b></td></td>
</tr>
<td><b>320</b></td><td><b><a href='list_command_service.php?id=320&tri=1 <view-source:http://serveur_cible/list_command_service.php?id=320&tri=1>'>3</b></td><td><b>ASU-CHECK-SPECIFIQUE-DISK</b></td><td><b>$USER1$/wrap.pl '$HOSTNAME$' '$SERVICEDESC$' '$HOSTDOWNTIME$' '$SERVICEDOWNTIME$' $USER1$/check_toto -t 30 -n -H $HOSTADDRESS$ -c $ARG1$ -a "$ARG2$ $ARG3$ $ARG4$"</b></td><td><b></b></td><td><b></b></td><td><b>2</b></td></td>
</tr>


Je souhaite extraire plusieurs informations.
Test pour l'instant d'extraction
- pour ( $USER1$/check_nrpe ), l'expression régulière ' \$USER1\$\/(?<plugin>[^ ]+)' me permet d'extraire check_nrpe

Par contre, pour (id=320&tri=1>'>0</b></td><td><b>ASU-CHECK-SPECIFIQUE-DISK</b></td>),
comment faire pour extraire
320
le chiffre, ici 3, situé entre &tri=1>'>3</b></td>
les chiffres ou lettres, ici ASU-CHECK-SPECIFIQUE-DISK , situés entre <td><b>ASU-CHECK-SPECIFIQUE-DISK</b></td>

Résultat escompté:
id=321 countservice=0 template=A-CHK-PMU-ANALYSLOG plugin=check_nrpe
id=322 countservice=1234 template=A-CHK-PMU-ORACLE-CNX plugin=check_nrpe
id=320 countservice=3 template=ASU-CHECK-SPECIFIQUE-DISK plugin=check_toto

Pour mes tests, j'utilise REGEX STORM.

Merci de votre aide,

jean-marc

2 réponses

cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
21 juin 2018 à 14:10
In progress !!!

Création classe
Imports System.Text.RegularExpressions

Public Class ExtractIDSTATE

    Public Sub New()
    End Sub

    Public Sub New(ByVal M As Match)
        'Id = M.Groups("id").Value
        'Countservice = M.Groups("countservice").Value
        'Template = M.Groups("template").Value
        Plugin = M.Groups("plugin").Value
    End Sub

    Public Property Id() As Integer
    Public Property Countservice() As Integer
    Public Property Template() As String
    Public Property Plugin() As String

    Public Overrides Function ToString() As String
        Return Id & " - " & Countservice & " - " & Template & " - " & Plugin
    End Function

End Class


et

    Sub InitializeDownload()
        ' Download de la source ID-State dans la variable texte
        WebIDState.Navigate(URL_IDSTATE)
        While Not WebIDState.ReadyState = WebBrowserReadyState.Complete
            System.Windows.Forms.Application.DoEvents()
        End While

        Dim texte As String = WebIDState.DocumentText.ToString()
        If CDbl(texte.Length.ToString) = 1004 Then
            MessageBox.Show("Page Web ID-State non récupérée")
            Exit Sub
        End If
        Thread.Sleep((100))

        ' Extraction des données à partir de la variable texte
        'Dim pattern As String = "addIndex\('(?<trigramme>[A-Z0-9-]{3}) : (?<client>[^']+)','(?<serveur>[^']+)"

        Dim pattern As String = "  \$USER1\$\/(?<plugin>[A-Za-z0-9-_.]+) "

        'Dim pattern As String = "<td><b>(?<id>[0-9]+)  \$USER1\$\/(?<plugin>[A-Za-z0-9-_.]+) "

        ' MatchCollection n'est pas "Linquable" directement, d'où le Cast<Match>
        Dim toutesLesLignes As IEnumerable(Of ExtractIDSTATE) = (
            From m In Regex.Matches(texte, pattern).Cast(Of Match)()
            Where m.Success
            Select New ExtractIDSTATE(m))

        'MessageBox.Show(toutesLesLignes.Count.ToString)

        ' Liste des plugins
        ' Private listofplugin As List(Of ExtractIDSTATE)
        listofplugin = (
            From l In toutesLesLignes
            Group l By l.Plugin Into leGroup = Group Order By Plugin
            Select New ExtractIDSTATE With {.Plugin = leGroup.First().Plugin}).ToList()

        For Each myplugin In listofplugin
            MessageBox.Show(myplugin.Plugin, "myplugin")
        Next

        MessageBox.Show("coucou")
        Me.Close()
    End Sub


Je coince toujours pour créer mon pattern !!!

jean-marc
0
Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 656
22 juin 2018 à 00:11
Salut
id=(?<id>\d+)&tri=\d+>'>(?<countservice>\d+)</b></td><td><b>(?<template>.+)</b></td><td><b>\$.+\$USER1\$\/(?<plugin>[^ ]+)

0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
22 juin 2018 à 13:53
Salut Whismeril,

Ton pattern testé avec "RegEx Storm" me renvoie correctement les quatre variables (${id}, ${countservice}, ${template} et ${plugin} attendues avec le fichier tronqué mis en exemple.

Par contre, testé avec "mon" script VB.Net et le fichier complet téléchargé de mon serveur distant, c'est NOK !!!

Merci,
Je vais continuer à tester et te tiens au courant.

jean-marc
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27 > cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018
Modifié le 22 juin 2018 à 20:14
Whismeril,

<td><b>314</b></td><td><b><a href='list_command_service.php?id=314&tri=1 <view-source:http://serveurcible/idstate/list_command_service.php?id=314&tri=1>'>0</b></td><td><b>CHK-OS-ESX-CLI-memory</b></td><td><b>$USER1$/wrap.pl '$HOSTNAME$' '$SERVICEDESC$' '$HOSTDOWNTIME$' '$SERVICEDOWNTIME$'  $USER1$/esx/check_centreon_esx_memory.pl --server $_HOSTVCENTER$ --host $HOSTNAME$ --username $_HOSTESXUSER$ --password $_HOSTESXPASSWORD$ --warning $_SERVICEWARNING$ --critical $_SERVICECRITICAL$</b></td><td><b></b></td><td><b></b></td><td><b>2</b></td></td>
</tr>

Le terme "id=" étant plusieurs fois sur la ligne courante d'extraction, comment écrire le pattern pour prendre en compte "idstate/list_command_service.php?id=" ???

Via RegEx Storm, en mettant dans le pattern
idstate.+id=(?<id>\d+)&tri=\d+>'>(?<countservice>\d+)</b></td><td><b>(?<template>.+)</b></td><td><b>\$.+\$USER1\$\/(?<plugin>[^ ]+)
, les 4 champs s'affichent.

Par contre, en mettant dans le script
        MessageBox.Show(texte.Count.ToString, "me retourne environ 100.000")

        ' Extraction des données à partir de la variable texte
        Dim pattern As String = "idstate.+id=(?<id>\d+)'>(?<countservice>\d+)</b></td><td><b>(?<template>.+)</b></td><td><b>\$.+\$USER1\$\/(?<plugin>[^ ]+)"

        ' MatchCollection n'est pas "Linquable" directement, d'où le Cast<Match>
        Dim toutesLesLignes As IEnumerable(Of ExtractIDSTATE) = (
            From m In Regex.Matches(texte, pattern).Cast(Of Match)()
            Where m.Success
            Select New ExtractIDSTATE(m))

        MessageBox.Show(toutesLesLignes.Count.ToString, "me retourne 0")


Bonne soirée,
jean-marc
0
Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 656
24 juin 2018 à 21:10
Salut Jean-Marc.
le pattern que tu as essayé dans RegexStorn n'est pas exactement le même dans le code VB.

Je ne sais pas où se situe la différence, mais quand j'ai copié l'un dans l'autre j'ai eu ça
0
Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 656 > Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024
24 juin 2018 à 21:12
trouvé, il manque
&tri=\d+>'
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27 > Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024
Modifié le 24 juin 2018 à 21:25
Bonsoir Whismeril,

J'allais te répondre !!!
J'ai testé toute la journée, pas à pas avec un fichier et trouvé l'anomalie.

Le bon pattern est
Dim pattern As String = "id=(?<id>\d+)&tri=1'>(?<countservice>\d+)</b></td><td><b>(?<template>.+)</b></td><td><b>\$.+\$USER1\$\/(?<plugin>[^ ]+)"

        ' MatchCollection n'est pas "Linquable" directement, d'où le Cast<Match>
        Dim toutesLesLignes As IEnumerable(Of ExtractIDSTATE) = (
            From m In Regex.Matches(texte, pattern).Cast(Of Match)()
            Where m.Success
            Select New ExtractIDSTATE(m))

        ' Liste des plugins
        Dim listofplugins = (
            From l In toutesLesLignes
            Group l By l.Id Into leGroup = Group Order By Id
            Select New ExtractIDSTATE With {.Id = leGroup.First().Id, _
                                            .Countservice = leGroup.First().Countservice, _
                                            .Template = leGroup.First.Template, _
                                            .Plugin = leGroup.First.Plugin}).ToList()

        For Each myplugin In listofplugins
            MessageBox.Show("Id=" & myplugin.Id & Environment.NewLine & _
                            "Countservice=" & myplugin.Countservice & Environment.NewLine & _
                            "Template=" & myplugin.Template & Environment.NewLine & _
                            "Plugin=" & myplugin.Plugin, "myplugin")
        Next


Merci encore et bonne soirée,
jean-marc
0
Rejoignez-nous