Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Replacing whitespace with null

by kelan (Deacon)
on Oct 11, 2002 at 23:22 UTC ( [id://204700]=note: print w/replies, xml ) Need Help??


in reply to Replacing whitespace with null

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-25 19:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found