in reply to Re: '|' need to be escaped?
in thread '|' need to be escaped?
Basically a split on (' ',$_) suppresses the leading NULL field if there is a leading white space character. I don't know of a split on a regex variation of \s that can do the same thing. Do you?
So when the first arg to split() is in single quotes ...split (' ',$_) it is different than a regex...#!/usr/bin/perl -w use strict; my $string = " abc xyz"; my @tokens = split(' ',$string); print "There are ".@tokens." tokens in \'$string\'\n"; print "Using split on ' '\n"; print join("|",@tokens),"\n\n"; my @tokens2 = split(/\s+/,$string); print "There are ".@tokens2." tokens in \'$string\'\n"; print "Using split on /\\s+/\n"; print join("|",@tokens2),"\n\n"; my @tokens3 = split(/ /,$string); print "There are ".@tokens3." tokens in \'$string\'\n"; print "Using split on / /\n"; print join("|",@tokens3),"\n\n"; my @tokens4 = split(/\s/,$string); print "There are ".@tokens4." tokens in \'$string\'\n"; print "Using split on /\\s/\n"; print join("|",@tokens4),"\n"; __END__ The above code prints: There are 2 tokens in ' abc xyz' Using split on ' ' abc|xyz There are 3 tokens in ' abc xyz' Using split on /\s+/ |abc|xyz There are 6 tokens in ' abc xyz' Using split on / / ||||abc|xyz There are 6 tokens in ' abc xyz' Using split on /\s/ ||||abc|xyz
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: '|' need to be escaped?
by ikegami (Patriarch) on Sep 12, 2011 at 04:07 UTC |