Solved

Help with Parsing a string

Posted on 2007-11-27
7
198 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

685 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