Extracting data from a string

Status
Not open for further replies.
No, using regex is overkill. I suggest you do some research in to how regular expression engines work internally. As I said before, you can use regex only in very (very very) simple situations. As soon as you try to use it for extracting dynamic data from multiple locations in a document you're screwed with regex. It ain't rocket science.
 
I know how regex works. The way DOM parser works is simple but uses lot of memory and is slower. I find it trivial to waste resources for a simple task such as his. He does not need to extract dynamic data. Using a DOM parser is overkill and waste of resources in this case. It ain't rocket science either.
 
I know how regex works.

I don't think you do. If you did you'd agree with me and everyone esle who calls himself a coder.

Have fun: http://swtch.com/~rsc/regexp/regexp2.html

The way DOM parser works is simple but uses lot of memory and is slower.

Sigh. To parse XML/HTML one only needs a simple state machine. Because of that it can be parsed with relatively little code and is much more efficient when compared to the complex finite state machines that make up modern regular expression engines. It requires less CPU cycles than running it trough one or more regular expressions and because of the simplicity of the state machine it uses less memory. Once a DOM is parsed it'll use roughly the same amount of memory as the original string that holds the markup (+ a few KB here and there for the objects).

Every developer that needs to work with HTML or XML will parse it with state machine and not with regular expressions for said reasons.

I'll give you another article: http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html

I find it trivial to waste resources for a simple task such as his. He does not need to extract dynamic data.

Erm, you save resources if you simply parse the HTML the way it should be parsed. You might want to re-read the first post. It's clear that he needs dynamic data.

It ain't rocket science either.

Apparently for some it is. You know you're wrong here, why not just say I'm right? It's not gonna make you look stupid or anything. We all learn, including me ;).

PS: do read those articles.
 
Well from the looks of it, it doesn't look like he wants to parse data but extract it.
The regular expression reading was very interesting - thanks for the link :) I am also not clear as to what do you mean by dynamic data?
 
I am not doing by Hyper'z method or Regex method. Both are new to me, what I require is very simple work, just extracting all links with a given start string and end string from a page which I have already stored as string.

I am using loop and IndexOf of do it.
 
^That too is how it should not be done. It's virtually the same as regular expressions but even less solid. What is so hard about just parsing the HTML file? Why use some confusing dirty method? Anyway, it's your problem. Just know that your code is flawed.

Well from the looks of it, it doesn't look like he wants to parse data but extract it.
The regular expression reading was very interesting - thanks for the link :) I am also not clear as to what do you mean by dynamic data?

Lol you just don't give up do you ^^. You need to parse data to extract it correctly. By dynamic data I mean data that changes (coming from a PHP file for example). If you look at the 1st post you see he extracts data from a forum which is about as dynamic as it gets.
 
I already completed coding the part for extracting the string. It was not simple <div> and </div> tag. I had to extract data between two definite pattern of strings.

So, I didn't want to waste another week learning your method.
I'll give it time after I complete my project and learn that too though.

PHP:
 for(int i=0;i<5;i++)
            {                                        
                   string result = "";
            int iIndexOfBegin = strSource.IndexOf(strBegin);
            if (iIndexOfBegin != -1)
                            {
                String tempstring = strSource.Substring(iIndexOfBegin + strBegin.Length);
                int iEnd = tempstring.IndexOf(strEnd);
                if (iEnd != -1)
                {
                    result = tempstring.Substring(0, iEnd);
                
                    string next = result;
 
What is there to learn about:
Code:
var html = new HtmlDocument();

// load the html
html.LoadHtml(yourHtmlHere);

// use XPath to select all "A" elements from the html
var anchors = html.DocumentNode.SelectNodes("//a");

// filter out those that start with http
var filter = from a in anchors
             where a.GetAttributeValue("href", "").StartsWith("http")
             select a;

??

It's just loading a dll and calling a few methods. I don't see what needs to be learned here. What you're doing right there is the wrong way to do it and I wouldn't be surprised if I see you making another topic because suddenly something stopped working or your program crashes.
 
Doing what pankaj wants is quickest when using IndexOf - iv been through them all, and thats what conclusion i came to, which is why i created my little "getstringInbetween" class.
 
But it is not reliable with dynamic data. You need access to the DOM. This is not a situation where execution time is important but rather one where the validity of the data is.
 
I know the idea behind IndexOf and your string-in-between thing. It is still the wrong way to solve this problem. Why do people make it complexer than it needs to be? As someone said in the article I posted: parsing HTML is a solved problem.
 
I am sure nothing would crash or anything because of this code because I am only using it in some part. But you are a professional coder and as you are saying it works fast, I want to try out this too.

Mind replacing my source code with your code. And tell me exactly where should I put that dll file. I currently have put that file to "My Documents\Visual Studio 2008\Projects\viewsrc\viewsrc" or should I move it to "My Documents\Visual Studio 2008\Projects\viewsrc\"

http://pastebin.com/1jfKMvXK

EDIT : I just saw Jay's involvement in this thread with his argument, lets see who is proved right, Hyperz, Jay or Dman.
 
Just extract the .dll and add a reference to it in your project. Where you put the dll is not important.

Edit:
faae00a.png


d96000b.png


Edit 2:
I already proved I'm right. Do I need to post another 100 articles?
 
And can ya replace my above code with your code. I need to extract all strings between
PHP:
<h3 class="r"><a href=" and " class=l
</span>
 
It would help if your code made sense. What are you trying to do? From the looks of it you're using Google to try and find mp3's. Bad idea (using Google).

Edit: now I just have to guess what error.
 
Edit: now I just have to guess what error.
I didn't understand.

I tried few other ways than GOOGLE
1. I tried to search mp3's directly from sites like songs.pk but it wasn't possible because they use Google search , so I can't get definite results.
2. Even I tried 4shared but it too didn't help much.
3. mp3realm was a easy one but it blocked me identifying me as a bot.

Lastly, I consulted another friend of mine, he said to find using Google.

I see you are using Skeemr, are you using skeemr directly or bought API ?

About the use of GetStringmethod or Regex menthod or your method. I guess I'll better continue with GetString method which I am using, I dont see how it can mess up anything. And I don't know how to use it too nicely.
Though your solution is a solved one in that article, I don't want to be lazy. I already made that code and will continue to use it.
NOTE : Jay yet has to give his examples to prove he is right.
 
Status
Not open for further replies.
Back
Top