Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Splitting string using two overlapping patterns

by sundialsvc4 (Abbot)
on Oct 24, 2013 at 23:59 UTC ( #1059565=note: print w/replies, xml ) Need Help??

in reply to Splitting string using two overlapping patterns

When I look at situations like this, I get really nervous that the incoming file might be even more inconsistent than I thought it was ... and that my “clever regex” solution might be less-robust than I need it to be.   I would not be confident that my code is, in fact, a verifiably-correct answer, due to the “clever regex.”   So, what I would probably choose to do, is to use a loop, and to break the string down right-to-left, pushing the pieces onto a stack-array as I parsed them.   For example:

while ($str ne '') { $str =~ s/^\s+//; # REMOVE LEADING WHITESPACE last if ($str eq ''); # LEAVE LOOP EARLY IF IT WAS ALL-WHITESP +ACE if ($str =~ /^\{/) { # STARTS WITH '{' ... elsif ... ...
Well, you get the idea, I think.

Even though this code might-or might-not be “efficient,” I am fairly confident that I could debug it, and that I could extend it to cover new cases and be confident (a) that the new changes work, and that (b) I didn’t break something in the process.

Replies are listed 'Best First'.
Re^2: Splitting string using two overlapping patterns
by bioinformatics (Friar) on Oct 25, 2013 at 04:54 UTC

    It looks like he's parsing the headers from output of a specific program, so it ought to be consistent. Your method and other suggestions should work, but the way he's looking to do it should be fine. This isn't large-scale logfile parsing...


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1059565]
[Discipulus]: if you hit the fan the cpu can overheat..
shmem fixed sporadic temperature watchdog resets on his laptop by cleaning the fan

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-04-24 11:37 GMT
Find Nodes?
    Voting Booth?