in reply to Re: Re: Re: Re: How to remove the $1 hard coding
in thread How to remove the $1 hard coding

I was going to assert that this would generally be equivalent to splitting on whitespace, with the obvious difference that, if the string began with whitespace, split would return a list that included an empty string as the first element -- the first element returned by the regex would be the second element returned by split.

You obviously didn't test it :-) split ' ' (as posted) is MAGICAL. It is not the same as split /\s*/.

@v = split ' ', ' watch the magic '; print "$_: $v[$_]\n" for 0..$#v; print $/; @v = split /\s+/, ' lost the magic '; print "$_: $v[$_]\n" for 0..$#v; __DATA__ 0: watch 1: the 2: magic 0: 1: lost 2: the 3: magic

As you see it is absolutely identical to the posted RE and drops leading whitespace.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
('Re: ' x 6) How to remove the $1 hard coding
by MidLifeXis (Monsignor) on Aug 23, 2003 at 20:40 UTC

    The original RE is m/\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*/. If this is equivilent to splitting on ' ', then I have some serious remedial RE work to do.

    The original RE will match 'ABCD' and split it into 4 characters. Your solution using split will not. I am not sure that the original poster is 100% certain about what the initial RE does, and your solution may match intent, but as written the are not the same.

    Now, since you are using the pattern \s+ as your split, then your splits are nearly equivilant (leading space and trailing space aside). However, your split is not equivilant to the original RE.

    Nothing personal, just technically misleading. All of the info was correct (++), just not related to the OP (--).

      Sure - you are of course correct. However like you I am almost positive the poster has no idea how that RE will behave, vis:

      @res = 'IS~THISOK?' =~ m/\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*/; print "@res\n"; __DATA__ IS~THIS O K ?

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print