Solved

Help with Parsing a string

Posted on 2007-11-27
7
195 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Registering DLL 5 43
Data is not showing from images 15 36
regx  exclude  pattern 6 26
Displaying a web form as a modal popup dialog box. 4 16
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…

937 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now