Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Regex Parsing

by toadi (Chaplain)
on Mar 27, 2005 at 11:06 UTC ( #442608=perlquestion: print w/replies, xml ) Need Help??
toadi has asked for the wisdom of the Perl Monks concerning the following question:

I really stink in writing regex.
my $data=<<"DATA"; BEGIN:01;BE;1000 Match This Match That SOMEWHERE,28 1000 BRUSSEL 1 Belgique DATA

I loop over a file where i split on formfeeds filling the $data scalar. When it is filled i want to parse "parse this" and "parse that" out of it.

There is more in the $data I needed to parse out, but i succeeded in them, but this gives me more troubles...

My opinions may have changed,
but not the fact that I am right

Replies are listed 'Best First'.
Re: Regex Parsing
by cog (Parson) on Mar 27, 2005 at 13:17 UTC
    I see what you're trying to accomplish (I think), but I can't see what you've tried...

    How is it giving you trouble? What code do you have that is not working?

      Did not really have any code. Did not have a idea howto do it.
      But after some more pondering:
      my @array = (split/\n/, $data); my $firstmatch = $array[3]; my $secondmatch = $array[4];

      But is this the best/fastes way. Got lots of files with this kind of data.

      This happens over and over:

      Open the file split the records parse this data insert it into db close file

      My opinions may have changed,
      but not the fact that I am right

        I think you have an excellent solution using split and array access. There is no reason to use a regex when other solutions will work equally well. Often, a regex will be more complicated or more fragile.

        Now, there are some parsing problems that are better solved with a regex, but this isn't one of them. And, personally, I only use a regex when I cannot use other solutions.

        Being right, does not endow the right to be rude; politeness costs nothing.
        Being unknowing, is not the same as being stupid.
        Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
        Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Regex Parsing
by jhourcle (Prior) on Mar 27, 2005 at 12:35 UTC

    What qualifies as parsing 'out of it' ? Are you trying to remove it from the string, or pull it out for use in something else (without modifying the original string)? Do you want to remote it, and save it for use? If you remove it, and it was the only thing on the line, do you also want to remove the now blank line?

    Also, could you show your loop? You didn't say if you're working on the data in place, or moving into some other strcture.

Re: Regex Parsing
by tlm (Prior) on Mar 27, 2005 at 14:51 UTC

    I'm sure we could solve this problem, but I'm afraid you are trying to reinvent a thoroughly invented wheel. What you need is something like Text::Template or the Template Toolkit from CPAN. They do exactly what you are trying to implement. You'll save loads o' time, and have the leisure to go out and feast on pommes frites.

    the lowliest monk

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://442608]
Approved by moot
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2017-12-15 22:58 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (443 votes). Check out past polls.