RegEx Help

I need the reg regex to identify the image like below:

Image tag on the website:
<img width="142" height="185" alt="Agent Info" src="http://domain.com/image.jpg">

Using this code:
Dim mch As System.Text.RegularExpressions.Match = _
            System.Text.RegularExpressions.Regex.Match(mystring, "<img.*id=\x22Master_Leftnav1_AgentContactInfo_AgentPhoto\x22*.?src=\x22(.*?)\x22")
azyet24Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
theplonkCommented:
Try the following regex:
Dim mch As System.Text.RegularExpressions.Match = _
                    System.Text.RegularExpressions.Regex.Match(Me.TextBox1.Text, "(?<=img.*src\=[\x27\x22])(?<Url>[^\x27\x22]*)(?=[\x27\x22])")

Open in new window

0
 
azyet24Author Commented:
alt="Agent Info" is what I'll have to look for so this should be in the expression.  What I want to get is the url so I can download the image.
0
 
azyet24Author Commented:
I was trying something like this but it doesn't work and I don't know regex:

"<img.*alt=\x22Agent.*Info\x22*.?src=\x22(.*?)\x22"
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
theplonkCommented:
Sorry try this:
System.Text.RegularExpressions.Regex.Match(Me.TextBox1.Text, "(?<=img.*alt\=[\x27\x22]Agent\sInfo[\x27\x22].*src\=[\x27\x22])(?<Url>[^\x27\x22]*)(?=[\x27\x22])")

Open in new window

0
 
theplonkCommented:
Otherwise if the format of the img tag changes in order/ is inconsistant. i.e. the order of "alt" and "src" are differnt.

You can try the solution that is posted here:
http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/Q_23964786.html
0
 
Fernando SotoRetiredCommented:
Hi azyet24;

The Regex pattern in the code snippet should do what you want.

Fernando
Imports System.Text.RegularExpressions
 
'======================================================================================================
' Test Data
Dim inputData As String = "Image tag on the website:" & _
        "<img width=""142"" height=""185"" alt=""Agent Info"" src=""http://domain.com/image.jpg""> " & _
        "Using this code:"
'======================================================================================================
 
Dim pattern As String = "<img.*?alt=\x22([^\x22]+)\x22.*?src=\x22([^\x22]+)\x22"
Dim m As Match = Regex.Match(inputData, pattern)
 
'======================================================================================================
' Display results
If m.Success Then MessageBox.Show("alt = " & m.Groups(1).Value & "  URL = " & m.Groups(2).Value)
'======================================================================================================

Open in new window

0
 
azyet24Author Commented:
Fernando, you code works with your sample data, but is not working with the website so I'm not sure what the problem is.  Here is an example link:
"http://www.domain.com/Websites/transform.php?agent=SomeName"
The code should download the agents picture.
Please grab this b/c I will need to have this removed from EE.
0
 
Fernando SotoRetiredCommented:
Hi azyet24;

Can you post the code you used.

Also how many of these type of <img ... > will ever be in the data?

Fernando
0
 
azyet24Author Commented:
There will be tons of img tags in the data, but only one will have alt="Agent Info"
Try
 
            Dim client As New System.Net.WebClient()
            Dim mystring As String = client.DownloadString(agenturl)
 
            Console.WriteLine(name & " " & counter)
            Dim mch As System.Text.RegularExpressions.Match = _
                  System.Text.RegularExpressions.Regex.Match(mystring, "<img.*?alt=\x22([^\x22]+)\x22.*?src=\x22([^\x22]+)\x22")
 
            Dim pattern As String = "<img.*?alt=\x22([^\x22]+)\x22.*?src=\x22([^\x22]+)\x22"
            Dim m As Match = Regex.Match("http://SomeName.domain.com/Websites/transform.php?agent=SomeName", pattern)
 
            If m.Success Then Console.WriteLine("alt = " & m.Groups(1).Value & "  URL = " & m.Groups(2).Value)
 
            If mch.Success Then
                If mch.Groups(1).Value <> "" Then
                    Dim WebC As New WebClient
                    Try
 
                        WebC.DownloadFile(mch.Groups(2).Value, localfolder & "RBI_" & name & id & ".jpg")
                        Console.WriteLine(mch.Groups(2).Value & " photo downloaded")
                       
                    Catch ex As Exception
                        Console.WriteLine("Error " & ex.Message)
                    End Try
                End If
            End If
        Catch ex As Exception
            Console.WriteLine("Error " & ex.Message)
        End Try

Open in new window

0
 
Fernando SotoRetiredCommented:
Hi azyet24;

I have modified your code and it works for me, try it this way.

Fernando
Dim pattern As String = "<img.*?alt=\x22([^\x22]+)\x22.*?src=\x22([^\x22]+)\x22"
Try
 
    Dim client As New System.Net.WebClient()
    Dim mystring As String = client.DownloadString(agenturl)
 
    Console.WriteLine(Name & " " & counter)
    Dim mch As System.Text.RegularExpressions.Match = _
          System.Text.RegularExpressions.Regex.Match(mystring, pattern)
 
    If mch.Success Then Console.WriteLine("alt = " & mch.Groups(1).Value & "  URL = " & mch.Groups(2).Value)
 
    If mch.Success Then
        If mch.Groups(1).Value <> "" Then
            Dim WebC As New WebClient
            Try
 
                WebC.DownloadFile(mch.Groups(2).Value, localfolder & "RBI_" & Name & id & ".jpg")
                Console.WriteLine(mch.Groups(2).Value & " photo downloaded")
 
            Catch ex As Exception
                Console.WriteLine("Error " & ex.Message)
            End Try
        End If
    End If
Catch ex As Exception
    Console.WriteLine("Error " & ex.Message)
End Try

Open in new window

0
 
azyet24Author Commented:
In your code, where are you isolating alt="Agent Info"?
0
 
Fernando SotoRetiredCommented:
In the regex pattern below this part, alt=\x22([^\x22]+)\x22", matches and img tag that has a alt parameter.

Dim pattern As String = "<img.*?alt=\x22([^\x22]+)\x22.*?src=\x22([^\x22]+)\x22"

If the string in the alt parameter is always going to be "Agent Info" then you can use this regex pattern in place of the above one.

Dim pattern As String = "<img.*?alt=\x22(Agent Info)\x22.*?src=\x22([^\x22]+)\x22"

0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
azyet24Author Commented:
not working for me.  complete code below.
Getrbiimages(""http://www.domain.com/Websites/transform.php?agent=SomeName"", "User Name", 1)
 
    Sub Getrbiimages(ByVal agenturl As String, ByVal name As String, ByVal id As Integer)
        Dim pattern As String = "<img.*?alt=\x22(Agent Info)\x22.*?src=\x22([^\x22]+)\x22"
        Try
 
            Dim client As New System.Net.WebClient()
            Dim mystring As String = client.DownloadString(agenturl)
 
            Console.WriteLine(name & " " & counter)
            Dim mch As System.Text.RegularExpressions.Match = _
                  System.Text.RegularExpressions.Regex.Match(mystring, pattern)
 
            If mch.Success Then Console.WriteLine("alt = " & mch.Groups(1).Value & "  URL = " & mch.Groups(2).Value)
 
            If mch.Success Then
                If mch.Groups(1).Value <> "" Then
                    Dim WebC As New WebClient
                    Try
 
                        WebC.DownloadFile(mch.Groups(2).Value, localfolder & "RBI_" & name & id & ".jpg")
                        Console.WriteLine(mch.Groups(2).Value & " photo downloaded")
 
                    Catch ex As Exception
                        Console.WriteLine("Error " & ex.Message)
                    End Try
                End If
            End If
        Catch ex As Exception
            Console.WriteLine("Error " & ex.Message)
        End Try
 
        Console.ReadLine()
    End Sub

Open in new window

0
 
azyet24Author Commented:
Sorry, that did work
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.