Solved

Help with Parsing a string

Posted on 2007-11-27
7
196 Views
Last Modified: 2010-04-23
Here is the string that i need to parse.
here is the string i need to parse and break into.

<linkCodeHTML>&lt;a href="http://www.dpbolvw.net/click-1610923-10304531">&lt;img src="http://www.lduhtrp.net/image-1610923-10304531" width="120" height="600" alt="null" border="0"/>&lt;/a></linkCodeHTML>



The above string consists of 4 parts
the url which is http://www.dpbolvw.net/click-1610923-10304531
and from the URL i need to get the value
1610923 and 10304531 to a variable.
And ImageURL to http://www.lduhtrp.net/image-1610923-10304531

Is there a simple function that i could use to retrieve the 3 values to a three different variables, ( ie url , x, y, ImageURL)
i want the final values to be

url="http://www.dpbolvw.net/click-1610923-10304531"
x=1610923
y=10304531
ImageURL="http://www.lduhtrp.net/image-1610923-10304531"


Thanks
0
Comment
Question by:TECH_NET
7 Comments
 
LVL 7

Expert Comment

by:sj_hicks
ID: 20363308
I believe that regular expressions should be able to do that.  Unable to help you with the regex syntax, as it's a bit of an art form and I'm not a regex artist!
0
 
LVL 12

Expert Comment

by:needo_jee
ID: 20363372
though best approach is regular expression but if URL is supposed to be same .. and not going to change then you may try

string url = "http://www.dpbolvw.net/click-1610923-10304531";
        string LastPart = url.Substring(url.LastIndexOf("/") + 1);
        string[] arrValues;
        arrValues = LastPart.Substring(LastPart.IndexOf("-") + 1).Split('-');
        string x = arrValues(0);
        string y = arrValues(1);
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20363447
The regex pattern:
<linkCodeHTML>.*?href=(?<URL>".*?").*?src=(?<imageURL>".*?image-(?<x>\d+)-(?<y>\d+)")

Results in these match groups with your test string:
Array
(
    [0] => Array
        (
            [0] => <linkCodeHTML>&lt;a href="http://www.dpbolvw.net/click-1610923-10304531">&lt;img src="http://www.lduhtrp.net/image-1610923-10304531"
        )

    [URL] => Array
        (
            [0] => "http://www.dpbolvw.net/click-1610923-10304531"
        )

    [1] => Array
        (
            [0] => "http://www.dpbolvw.net/click-1610923-10304531"
        )

    [imageURL] => Array
        (
            [0] => "http://www.lduhtrp.net/image-1610923-10304531"
        )

    [2] => Array
        (
            [0] => "http://www.lduhtrp.net/image-1610923-10304531"
        )

    [x] => Array
        (
            [0] => 1610923
        )

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

    [y] => Array
        (
            [0] => 10304531
        )

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

)

If you create a match collection with that pattern, the named groups would contain the matches you are looking for, you should be able to combine that into one function rather nicely.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 27

Expert Comment

by:ddrudik
ID: 20363466
Note that you will want to enable the Singleline and IgnoreCase options to help your matching of HTML in case of variances in the source.
0
 
LVL 1

Accepted Solution

by:
ajay_kanse earned 500 total points
ID: 20443605
Below code will work as per your requirement :

# Input string
$_='<linkCodeHTML>&lt;a href="http://www.dpbolvw.net/click-1610923-10304531">&lt;img src="http://www.lduhtrp.net/image-1610923-10304531" width="120" height="600" alt="null" border="0"/>&lt;/a></linkCodeHTML>';

#match the pattern
m/(.*)href\=\"(http\:\/\/\w+\.\w+\.\w+\/\w+)\-([0-9]+)\-([0-9]+)\"\>(.*)src\=\"(.*)\"\swidth/i ;

# extract desired output
print $_."\n" ;

print "URL=".$2."-".$3."-".$4."\n";
print "X=". $3."\n" ;
print "Y=". $4."\n" ;
print "ImageURL=". $6."\n" ;

Let me know if this works.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20534685
TECH_NET, did any of this help you?  Do you still need more information?
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question