IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

recherche le mot avec la position du mot.


Sujet :

VB.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2018
    Messages
    323
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2018
    Messages : 323
    Par défaut recherche le mot avec la position du mot.
    Bonjour,

    j'arrive pas à avoir la position du mot en recherchent du mot a avec File.ReadAllLines :
    ça me donne a la sortie du richtextbox1:
    Ligne 1 : a
    Ligne 1 : a
    Ligne 1 : a

    exemple :
    a
    b
    c
    a
    g
    h
    b
    a
    c
    g
    k
    ligne 1 : a ,ligne 4 : a , ligne 8 : a

    le code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    For Each line As String In File.ReadAllLines(filepath)
                Dim index As List(Of Integer) = counter(line, TextBox1.Text)
                If index.Count > 0 Then
                    RichTextBox1.AppendText(String.Format("Ligne {0} : {1} {2}", index.Count, TextBox1.Text, Environment.NewLine))
                End If
            Next
     
    Private Function counter(fichier As String, mot As String) As List(Of Integer)
            Dim positions As New List(Of Integer)
            Dim index As Integer = 0
            Dim words As String() = fichier.Split(New String() {Environment.NewLine, " "}, StringSplitOptions.RemoveEmptyEntries)
            For Each word As String In words
                If String.Compare(word, mot, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CompareOptions.IgnoreCase) = 0 Then
                    positions.Add(index)
                    index += 1
                End If
            Next
            Return positions
        End Function
    pouvez-vous m'aider à avoir la ligne du mot a à recherchent avec File.ReadAllLines? merci d'avance

  2. #2
    Membre Expert Avatar de Thumb down
    Homme Profil pro
    Retraité
    Inscrit en
    Juin 2019
    Messages
    1 585
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Juin 2019
    Messages : 1 585
    Par défaut
    Bonjour,
    Si un truc comme ça te convient!
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Imports VBScript_RegExp_55
    Module Module1
        Sub Main()
            Dim txt As String() = IO.File.ReadAllLines("C:\Myrep\Name.txt"), tx As String = ""
            For i = 0 To txt.Count - 1
                Dim t As Object = Occurences(txt(i), "jean")
                If t.Count > 0 Then
                    For ix = 0 To t.Count - 1
                        tx += $"L:={i + 1} C:={t.Item(ix).FirstIndex + 1} TX:={t.Item(ix).Value }{Environment.NewLine}"
                    Next
                End If
            Next
        End Sub
        Private Function Occurences(Source As String, ATrouver As String) As Object
            '*********************************************************************
            ' Auteur: Whismeril
            'lien: https://codes-sources.commentcamarche.net/profile/user/Whismeril
            '************************************************************************
            Dim rex As New VBScript_RegExp_55.RegExp
            rex.IgnoreCase = True 'on ignore la casse
            rex.Global = True 'on continue même après avoir trouvé une occurence
            rex.Pattern = ATrouver 'le modèle recherché est ATrouver
     
     
            Return rex.Execute(Source) 'on effectue la recherche
        End Function
    Code TXT : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    jean
    pierre
    pierre jean
    alain
    jean phillipe
    jean jean
    Code résultat : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    L:=1 C:=1 TX:=jean
    L:=3 C:=8 TX:=jean
    L:=5 C:=1 TX:=jean
    L:=6 C:=1 TX:=jean
    L:=6 C:=6 TX:=jean

  3. #3
    Membre éprouvé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2018
    Messages
    323
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2018
    Messages : 323
    Par défaut
    Bonjour, merci d’avoir répondu à mon problème. j'ai essayer ton code ne convient pas car j'ai pas dans référence : Microsoft VBScript Regular Expressions 5.5.dll dans VB.Net 2010.

    je vue dans un site la position des occurrences : https://stackoverflow.com/questions/...n-array-vb-net

    j'ai essayer chez moi ça fonctionne pas avec ReadAllLines pour avoir le nombre occurrence et la position occurrence du mot à rechercher :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    If Not String.IsNullOrEmpty(TextBox1.Text) Then
                Dim files1 As List(Of résult) = SearchText4(filepath, TextBox1.Text)
                If files1.Any Then
                    For Each f As résult In files1
                        RichTextBox1.AppendText(String.Format("Le fichier : {0} - Le mot à rechercher : {1} ({2} Résultats) {3}", f.Name, f.Pattern, f.Value, Environment.NewLine))
                        For i = 0 To f.Value - 1
                            RichTextBox1.AppendText(String.Format("Ligne {0} : {1} {2}", f.Offset, f.Pattern, Environment.NewLine))
                        Next
                    Next
                Else
                    RichTextBox1.AppendText(String.Format("Oups, Aucun texte à était trouver : {0}", TextBox1.Text))
                End If
            Else
                MsgBox("Veuillez entrer un texte pour rechercher !", MsgBoxStyle.Exclamation, "Attention")
            End If
    Private Function SearchText4(filename As String, mot As String) As List(Of résult)
            Dim data As New List(Of résult)
            For Each Line As String In File.ReadAllLines(filename)
                Dim caractères As Char() = Line.ToCharArray
                Dim index As List(Of Integer) = GetCount(caractères, mot)
                If index.Count > 0 Then
                    data.Add(New résult With {.Name = filename, .Pattern = mot, .Value = index.Count, .Offset = String.Join(" ", index)})
                End If
            Next
            Return data
        End Function
    Private Function GetCount(caractères As Char(), mot As String, Optional ByVal Count As Integer = 0, Optional value As Integer = -1) As List(Of Integer)
            Dim data As New List(Of Integer)
            Do
                value = Array.IndexOf(caractères, mot, Count)
                If value > -1 Then
                    Exit Do
                End If
                data.Add(value)
                Count = value + 1
            Loop Until Count = caractères.Length
            Return data.ToList
        End Function
    comment faire pour avoir le nombre occurrence du mot à rechercher et la position du mot de la recherche? merci d'avance

  4. #4
    Membre Expert Avatar de Thumb down
    Homme Profil pro
    Retraité
    Inscrit en
    Juin 2019
    Messages
    1 585
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Juin 2019
    Messages : 1 585
    Par défaut
    Bonjour,
    tu fais un click droit sur référence
    Nom : Sans titre.png
Affichages : 187
Taille : 88,1 Ko
    tu recherche la librairie
    Nom : Sans titre.png
Affichages : 182
Taille : 76,9 Ko
    puis tu valide

  5. #5
    Membre éprouvé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2018
    Messages
    323
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2018
    Messages : 323
    Par défaut
    Bonjour, merci d'avoir répondu mais grâce à toi. tu m'as donner la solution pour avoir la position des mots à rechercher avec file.readalllines.
    mais j'ai un petit souci avec mon code list(of integer) car ça me donne des doublons.
    voici en image ça me donne :
    Nom : Capture d’écran 2022-07-22 124715.png
Affichages : 171
Taille : 6,9 Ko
    le mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    Private Function SearchText5(filename As String, Mot As String) As List(Of résult)
            Dim data As New List(Of résult)
            Dim Pos As New List(Of Integer)
            action = False
            Dim txt As String() = IO.File.ReadAllLines(filename)
            If (txt.Length < 1) Then Throw New FileLoadException("Attention, Il n'y a rien dans le fichier !")
            For i = 0 To txt.Count - 1
                Dim Caractéres As Char() = txt(i).ToCharArray
                For j = 0 To Caractéres.Length - 1
                    If Caractéres(j) = Mot Then
                        Pos.Add(i + 1)
                        action = True
                    End If
                Next
            Next
            If action Then
                For i = 0 To Pos.Count - 1
                    data.Add(New résult With {.Name = Path.GetFileName(filename), .Pattern = Mot, .Value = Pos.Count, .Offset = Pos.Item(i)})
                Next
            End If
            Return data
        End Function
     
    If Not String.IsNullOrEmpty(TextBox1.Text) Then
                Dim files1 As List(Of résult) = SearchText5(filepath, TextBox1.Text)
                If files1.Any Then
                    For Each f As résult In files1.Distinct
                        RichTextBox1.AppendText(String.Format("Le fichier : {0} - Le mot à rechercher : {1} ({2} Résultats) {3}", f.Name, f.Pattern, f.Value, Environment.NewLine))
                        For i = 0 To f.Value - 1 Step 1
                            RichTextBox1.AppendText(String.Format("Ligne  {0} : {1} {2}", f.Offset, f.Pattern, Environment.NewLine))
                        Next
                    Next
                Else
                        RichTextBox1.AppendText(String.Format("Oups, Aucun texte à était trouver : {0}", TextBox1.Text))
                End If
            Else
                MsgBox("Veuillez entrer un texte pour rechercher !", MsgBoxStyle.Exclamation, "Attention")
            End If
    Public Class résult
        Public Property Name As String
        Public Property Pattern As String
        Public Property Value As Integer
        Public Property Offset As Integer
    End Class
    mon problème viens dans le code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    If action Then
                For i = 0 To Pos.Count - 1 '<= le problème viens ici
                    data.Add(New résult With {.Name = Path.GetFileName(filename), .Pattern = Mot, .Value = Pos.Count, .Offset = Pos.Item(i)})
                Next
            End If
    avec sens for i = 0 to pos.count - 1 ...etc
    Nom : Capture d’écran 2022-07-22 125146.png
Affichages : 172
Taille : 2,5 Ko

    le code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    data.Add(New résult With {.Name = Path.GetFileName(filename), .Pattern = Mot, .Value = Pos.Count, .Offset = Pos.Item(Pos.Count - 1)})
    comment puis je faire faire pour avoir dans pos en une seule fois le nombre de résultats à la fin? merci d'avance
    exemple :
    Le fichier : teste.txt - Le mot à rechercher : a (3 Résultats)
    Ligne 1 : a
    Ligne 4 : a
    Ligne 8 : a

  6. #6
    Membre Expert Avatar de Thumb down
    Homme Profil pro
    Retraité
    Inscrit en
    Juin 2019
    Messages
    1 585
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Juin 2019
    Messages : 1 585
    Par défaut
    bonsoir,
    ça serait pas un truc comme ça que tu souhaite par hasard?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
     Private Function SearchText5(filename As String, Mot As String) As List(Of résult)
            Dim data As New List(Of résult)
            Dim Pos As New List(Of Integer)
            Action = False
            Dim txt As String() = IO.File.ReadAllLines(filename)
            If (txt.Length < 1) Then Throw New FileLoadException("Attention, Il n'y a rien dans le fichier !")
            For i = 0 To txt.Count - 1
                Dim Caractéres As Char() = txt(i).ToCharArray
     
     
                If (From b In txt(i).ToCharArray Where b = Mot.ToLower Select b).Count > 0 Then
                    Pos.Add(i + 1)
                    Action = True
                End If
     
     
            Next
            If Action Then
                For i = 0 To Pos.Count - 1
                    data.Add(New résult With {.Name = Path.GetFileName(filename), .Pattern = Mot, .Value = Pos.Count, .Offset = Pos.Item(i)})
                Next
            End If
            Return data
        End Function
    Edite:
    personnellement j'ai toujours pas compris!
    dans ton poster #1 tu nous présente un ficher avec 1 caractère par ligne et dans ton code tu recherche un mot
    soit tu cherche un mot soit tu cherches un lettre !
    la chose ce complique si tu cherche la lettre "e" dans un mot voire dans une phrase!
    dans Jean Pierre on trouve 3 fois la lettre "e"
    dans mon exemple du poste#2 ou je travail sur la recherche de mots et pas de lettre on trouve
    Jean à la ligne 6 colonne 1 et en ligne 6 colonne 6
    tu devrais expliciter ta demande et nous fournir un exemple plus en adéquation avec ta problématique!

    L:=6 C:=1 TX:=jean
    L:=6 C:=6 TX:=jean

  7. #7
    Membre éprouvé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2018
    Messages
    323
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2018
    Messages : 323
    Par défaut
    Bonjour Thumb down,

    voici un exemple ce que je vous faire comme Notepad.
    Nom : Capture d’écran 2022-07-23 134441.png
Affichages : 152
Taille : 11,1 Ko

    mon problème vient dans ce code pour afficher la position du mot :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    If action Then
                    For i = 0 To Pos.Count - 1
                        data.Add(New résult(Path.GetFileName(fn), Mot, Pos.Count, Pos.Item(i)))
                    Next
                End If
    j'ai essayer plusieurs fois autre solution pour résoudre mais aucun succès pour avoir la ligne du mot.

    j'ai refait une mise à jour du code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    Dim FilePath As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Teste")
        Dim action As Boolean = False
     
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            If Not String.IsNullOrEmpty(TextBox1.Text) Then
                RichTextBox1.Clear()
                Dim files1 As List(Of résult) = SearchText(FilePath, TextBox1.Text)
                If files1.Any Then
                    files1.ForEach(AddressOf Résultats)
                Else
                    RichTextBox1.AppendText(String.Format("Oups, Aucun texte à était trouver : {0}", TextBox1.Text))
                End If
            Else
                MsgBox("Veuillez entrer un texte pour rechercher !", MsgBoxStyle.Exclamation, "Attention")
            End If
        End Sub
     
        Private Function SearchText(FileNames As String, Mot As String) As List(Of résult)
            Dim data As New List(Of résult)
            Dim Pos As New List(Of Integer)
            action = False
            Dim files As String() = Directory.GetFiles(FilePath, "*.*", SearchOption.AllDirectories)
            If (files.Length < 1) Then Throw New FileNotFoundException("Attention, il y a aucun fichier dans le dossier : " & FileNames)
            For Each fn As String In files
                Dim txt As String() = IO.File.ReadAllLines(fn)
                If (txt.Length < 1) Then Throw New FileLoadException("Attention, Il n'y a rien dans le fichier !")
                For i = 0 To txt.Count - 1
                    Dim Caractéres As Char() = txt(i).ToCharArray
                    If (From b In txt(i).ToCharArray Where b = Mot Select b).Count > 0 Then
                        Pos.Add(i + 1)
                        action = True
                    End If
                Next
                If action Then
                    For i = 0 To Pos.Count - 1
                        data.Add(New résult(Path.GetFileName(fn), Mot, Pos.Count, Pos.Item(i)))
                    Next
                End If
                Pos.Clear()
            Next
            Return data
        End Function
     
        Private Sub Résultats(ByVal p As résult)
            RichTextBox1.AppendText(String.Format("Le fichier : {0} - Le mot à rechercher : {1} ({2} Résultats) {3}", p.Name, p.Pattern, p.Value, Environment.NewLine))
            For i = 0 To p.Value - 1 Step 1
                RichTextBox1.AppendText(String.Format("Ligne  {0} : {1} {2}", p.Offset, p.Pattern, Environment.NewLine))
            Next
        End Sub
     
    End Class
     
    Public Class résult
        Public Property Name As String
        Public Property Pattern As String
        Public Property Value As Integer
        Public Property Offset As Integer
     
        Public Sub New(ByVal _Name As String, ByVal _Pattern As String, ByVal _Value As Integer, ByVal _Offset As Integer)
            Me.Name = _Name
            Me.Pattern = _Pattern
            Me.Value = _Value
            Me.Offset = _Offset
        End Sub

  8. #8
    Membre éprouvé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2018
    Messages
    323
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2018
    Messages : 323
    Par défaut
    salut tous le monde. je viens trouver la solution à mon problème avec plusieurs recherche.

    voici en image :
    Nom : Capture d’écran 2022-07-23 165304.png
Affichages : 150
Taille : 52,5 Ko

    le problème venez avec ce code Pos.item(i) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    If action Then
                    data.Add(New résult(Path.GetFileName(fn), Mot, Pos.Count, Pos.ToList))
                End If
                Pos.Clear() '<= effacer pour chaque fichier dans le dossier.
     
    Public Class résult
     
        Public Property Name As String
        Public Property Pattern As String
        Public Property Value As Integer
        Public Property Offset As List(Of Integer)
     
        Public Sub New(ByVal _Name As String, ByVal _Pattern As String, ByVal _Value As Integer, ByVal _Offset As List(Of Integer))
            Me.Name = _Name
            Me.Pattern = _Pattern
            Me.Value = _Value
            Me.Offset = _Offset
        End Sub
     
    Private Sub Résultats(ByVal p As résult)
            RichTextBox1.AppendText(String.Format("Le fichier : {0} - Le mot à rechercher : {1} ({2} Résultats) {3}", p.Name, p.Pattern, p.Value, Environment.NewLine))
            For i = 0 To p.Value - 1
                RichTextBox1.AppendText(String.Format("Ligne {0} : {1} {2}", p.Offset.Item(i), p.Pattern, Environment.NewLine))
            Next i
        End Sub

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Recherche de mot avec match approximatif
    Par nemoyth dans le forum MS SQL Server
    Réponses: 3
    Dernier message: 27/07/2010, 15h22
  2. [XL-2003] Recherche un mot dans un autre fichier excel avec vba
    Par alaoui_nizar dans le forum Excel
    Réponses: 5
    Dernier message: 19/04/2010, 17h37
  3. Recherche sur mots-clé avec fonction ET et OU
    Par gerard101 dans le forum Requêtes et SQL.
    Réponses: 1
    Dernier message: 03/01/2009, 11h51
  4. recherche de mot avec ou sans accent
    Par ddeee dans le forum ASP
    Réponses: 3
    Dernier message: 02/03/2006, 10h06

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo