David Gilden wrote:
In the following why am I getting '23'
Is 23 being treated as a string?


Not exactly. It is NOT being treated as a number if that is what you mean, all of $s is being treated as a string. Specifically \w matches any "word" character, which is any alphanumeric...so [A-Za-z0-9_]. If you want to match 'digits' then you want \d, if you want to match non-digits you want \D but be careful because whitespace is a non-digit as are the other special characters. So more than likely you want the alpha class, [A-Za-z].


#!/usr/bin/perl -w
use strict;



my $s = ' 23 one two three';


$s =~ s/(\w{1,10}).+/$1/;

print $s;

---

my $s = 'one two three';

$s =~ s/(\w)/$1/;

print $s;

Returns 'one two three', I was looking for it to match 'one' and stop!


Right, but that is because it is replacing the match in this case the single letter 'o' with that match, the single letter 'o'. So even if it had matched 'one' it still wouldn't have changed the whole string to 'one'. How about:


$s =~ s/(\w+)\s.*/$1/;

Some suggested reading:

perldoc perlretut
perldoc perlre
perldoc perlrequick (may only be in 5.8)

http://danconia.org


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to