From what I have read "^" means start at the beginning,

You're misinterpreting the meaning of ^, which is what is breaking your regex. From your language, you think ^ means to start at the beginning of the string and then go through it finding all the whitespace. That is slightly incorrect. The ^ is called an anchor, which is in this case is a start-of-string anchor. It anchors your match to the beginning of the string. Meaning, "for this pattern to match, the string must start with everything that comes next." Your string doesn't start with whitespace, it starts with "BPLIF". In this case what you'd want is simply:

s/\s+//g;
This will find stretches of whitespace anywhere in the string. Incidentally, if the string you want to perform substitution or matching on is already in $_, you don't need to use =~ to bind the search expression. You can just do the s/// or m// by itself (as I've done above), and it will default to matching against $_.

Also, see admiraln's post above about possibly using tr///.

kelan


Yak it up with Fullscreen ChatterBox


In reply to Re: Replacing whitespace with null by kelan
in thread Replacing whitespace with null by treebeard

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.