Yo guys heres my code:

Im trying to extract one line each time the sub is called and if no more lines re-set to 0 and start again

Problem is that it does not increase even tho i have set it as an array, still brings up he first line

My listbox is like (no spaces):

User1 : Pass1
User2 : Pass2

Try
            Dim I As Integer
            If I >= ListBox2.Items.Count Then
                MsgBox("Restarted Integer")
                I = 0
            Else

                Dim Regex As New Regex("\w+:\w+")
                For Each M As Match In Regex.Matches(ListBox2.Items.Item(I))
                    Dim User As String = M.Value.Split(":").GetValue(0)
                    Dim Pass As String = M.Value.Split(":").GetValue(1)
                    MsgBox("" & User & " " & Pass)
                Next
                I += 1
            End If
        Catch Exception As Exception
        End Try

Recommended Answers

All 4 Replies

Dim Regex As New Regex("\w+:\w+")

First: Regex is a reserved word, so it should not be the name of the variable.

Also, if you're going to use "Split", you do not need to use a Regex.
If you are going to use Regex, you do not need to use Split.

Mixing them is not a real problem, though.

Example:

Imports System.Text.RegularExpressions

Public Class Form1
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      ListBox1.Items.Add("fred:passFred")
      ListBox1.Items.Add("joe:passJoe")
      ListBox1.Items.Add("bill:passBill")
      ListBox1.Items.Add("sam:passSam")
   End Sub

   Private Sub bnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnCheck.Click
      If (ListBox1.Items.Count.Equals(0)) Then
         Return
      End If

      ' Technique 1 ------------------------------------------------
      Dim rxNamePass As New Regex("\w+:\w+")
      For Each s As String In ListBox1.Items
         If (rxNamePass.IsMatch(s)) Then
            Dim arr() As String = s.Split(":")
            Dim User As String = arr(0)
            Dim Pass As String = arr(1)
            MsgBox("[" & User & "] [" & Pass & "]")
         End If
      Next

      ' Technique 2 ------------------------------------------------
      Dim rxNamePass2 As New Regex("(?<name>\w+):(?<password>\w+)")
      For Each s As String In ListBox1.Items
         If (rxNamePass2.IsMatch(s)) Then
            Dim m As Match = rxNamePass2.Match(s)
            Dim User As String = m.Groups("name").Value
            Dim Pass As String = m.Groups("password").Value
            MsgBox("[" & User & "] [" & Pass & "]")
         End If
      Next
   End Sub
End Class

Also, you would need to prevent passwords with embedded colons as it would skew the results.

ah nice one fella. im wating to get one line at a time tho. so when i need to change details i just refer to this sub and it auto gets next line if its available if not start from 0

ive got this atm but does not work =/

Dim I As Integer
        Try
            If I >= ListBox2.Items.Count Then
                MsgBox("Restarted Integer")
                I = 0
            Else
                Dim Items(I) As Array
                Dim rxNamePass As New Regex("\w+:\w+")
                Dim s As String = ListBox2.Items(I)
                If (rxNamePass.IsMatch(s)) Then
                    Dim arr() As String = s.Split(":")
                    Dim User As String = arr(0)
                    Dim Pass As String = arr(1)
                    MsgBox("[" & User & "] [" & Pass & "]")
                End If
                I = I + 1
            End If
        Catch Exception As Exception
        End Try

That's fine as long as your "I" integer is of high enough scope that it does not keep getting reset.

ah rite its working now =/ lol cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.