in reply to Possible to have regexes act on file directly (not in memory)

OK, if I understand correctly, you can't make assumptions about the size of what the full regex would possibly capture. But can you at least make some assumptions about the size of the start tags and end tags? If you can do that, then reading the file by chunks is a perfectly workable solution.

You basically need 2 patterns that will match the start and the end tags (or more patterns if there can be several sorts of start and end tags). Then you implement in your code a state machine or a mini-parser that looks for the start tag; when you've found one, you capture everything that comes from the file until you reach the end tag, and start allover again if this is what you need. For managing the chunk boundaries, you just need a sliding window (as already discussed) that is as large as the maximal length of the start or end tags.

Edit: @ Nocturnus: because I spent some time reading the various comments, your last message just above was not on the page I was reading when I wrote this message (in other words, I loaded the page before you posted this last message). Therefore, my message is not an answer to your very last message, but rather to the previous ones.

  • Comment on Re: Possible to have regexes act on file directly (not in memory)

Replies are listed 'Best First'.
Re^2: Possible to have regexes act on file directly (not in memory)
by Nocturnus (Scribe) on May 05, 2014 at 06:28 UTC

    When mentioning the start tags and end tags, I was just giving an example to illustrate the problem at theoretical level. In this example, I wouldn't be allowed to make assumptions even on the size of the tags.

    In reality, anyways, the patterns are different from what I mentioned in the example. They are more complicated, but I will undoubtedly be able to write a state machine based mini-parser to solve the problem. This approach will make porting the respective application to other programming languages much easier; the disadvantage is that I will have to change the software if I am given other or further patterns to search for (right now, I don't have the time and interest for writing my own parser for some general search pattern language, so I will hard-code a search algorithm for each pattern into my mini-parser).

    Being able to run regexes directly on the source files would just have been a way which is by far more comfortable, faster (in the sense of "When is the software ready?") and general and which would allow for new search patterns without changing the software.

    Thank you very much,

    Nocturnus

      > They are more complicated, but I will undoubtedly be able to write a state machine based mini-parser to solve the problem.

      You are aware of pos, right?

      > In reality, anyways, the patterns are different from what I mentioned in the example.

      If you can't produce a "real" example its better to let this thread die now. Plz don't keep people speculating.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      update

      see also XY Problem

        As far as I have understood, pos won't help because it works in conjunction with regular expressions, and (as this thread has shown clearly enough), this always means loading to memory which is not an option in my case. Please correct me if I am wrong.

        While I appreciate any help and while I would like to thank you explicitly for taking the time, I can't resist commenting on the XY problem:

        I am not one of the guys who try to let others work for them. I will never shout "Hi everybody, I have problem X, please find as many solutions as possible to that problem for me". Instead, I am trying to work out a set of possible solutions, in this case writing my own parser which acts directly on the files being one of these and trying to use regular expressions which directly operate on the files being a second one.

        Since I know how to do the former, and since I didn't know how to do the latter or even if the latter is feasible at all, I just asked for the latter one.

        By full intent, I do not want others to solve my problems; instead, I am aware that the others' time is a precious resource and thus try to only ask the things I don't know and couldn't research myself after having put great efforts on it. IMHO, this is not only perfectly acceptable, but I honestly think that more people should act in this manner.

        Furthermore, at philosophical level, the solution to every problem is to not want to solve it. Thus, if I had given you the "root" problem, what would prevent you from saying: "Hey, this again is an example for people asking for Y when they mean X. You really don't need to solve that problem. Search for other customers or go to holidays with your family."?

        Finally, my question was clear, precise and specific; I think I even wouldn't have needed to give the example to understand the problem. As the others' contributions have shown, they have understood the essence of the problem very well, and I think that most of them did so as soon as having read the headline. People who don't understand the problem at theoretical or example level won't be able to help at all.

        Looking into a problem at theoretical or example level does not have anything to do with speculation.

        Thanks again,

        Nocturnus