Solved

Help with Parsing a string

Posted on 2007-11-27
7
194 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now