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

Re^3: Regular Expressions: Removing 'only' single spaces from a string

by GrandFather (Saint)
on Oct 20, 2005 at 23:54 UTC ( [id://501851]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Regular Expressions: Removing 'only' single spaces from a string
in thread Regular Expressions: Removing 'only' single spaces from a string

Did it though? Consider:

use warnings; use strict; my $str1= "It is very very cold now\n"; my $str2 = $str1; $str1 =~ s/(?<! ) (?! )//g; print $str1; $str2 =~ s/ (?! )//g; print $str2;

which prints:

Itisvery verycoldnow Itisvery verycoldnow

Which string is correct? The (?<! ) is a zero width look back negative assertion: it matches if there isn't a space before the current space being matched. The (?! ) is a zero width look ahead negative assertion: it matches if there isn't a space after the current space being matched. Together these ensure that only single spaces are matched: only match a space that doesn't have a space before it and doesn't have a space after it.

You should have a look at perlretut.


Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re^4: Regular Expressions: Removing 'only' single spaces from a string
by BioBoy (Novice) on Oct 21, 2005 at 07:57 UTC
    Thanks for that explaination.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-19 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found