Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Help me write a good reg-exp for this text

by hardburn (Abbot)
on Sep 05, 2003 at 15:59 UTC ( [id://289253]=note: print w/replies, xml ) Need Help??


in reply to Help me write a good reg-exp for this text

Is the data of fixed width? If so, you're better off with unpack:

# @LIST_OF_ENTRIES contains your data, one line per element my %description_hash; foreach my $entry (@LIST_OF_ENTRIES) { # Replace 20 with the number of characters in the value portion my ($value, $name) = unpack('A20 A*', $entry); $value =~ s/\A\s*//; $value =~ s/\s*\z//; $description_hash($name} = $value; }

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Care to explain s/\A\s*//?
by waxmop (Beadle) on Sep 05, 2003 at 16:59 UTC
    Hi - Thanks for the help. I don't understand the \A in s/\A\s*// however. Can you explain it to me?
      $ perldoc perlre
      The \A anchors the search at the beginning of the string. A ^ anchors a search at the beginning of the LINE in a string. Documentation is a wonderful thing.

      --
      [ e d @ h a l l e y . c c ]

        I don't understand why you want to act in such an obscure manner, because without the /m modifier, as here, there is no difference between /^\s*/ and /\A\s*/. So you might just as well use the far more familiar form, with the caret.

        The difference between /\Z/, /\z/ and /$/ is far more significant.

        Well, I do agree with you on the RTFM part... it's all in there. Looking there should become a more natural response, than asking here.

        I'd prefer

        s/^\s+//;
        anyway, as this wouldn't do an empty substitution if the string doesn't start with whitespace.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://289253]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found