Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with Parsing a string

Posted on 2007-11-27
7
Medium Priority
?
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Munawar Hussain
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

722 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