>>>> I have a string, "$a" for arguments sake, that contains a
>>
>>single
>>
>>>>word. The word will always have exactly 8 characters in it, most
>>
>>likely
>>
>>>>something like "ABCD1234". I need to split this up into two strings
>>
>>($b
>>
>><snip>
>>
>>>if (length $string == 8) # might as well check eh?
>>>{
>>> $string =~ /^([^\d]+)(.*)$/;
>>> my ($characterString, $numberString) = ($1, $2);
>>>}
>>
>>
>>There is one potential problem with this algorithm, which is that you
>>aren't checking to make sure that the string actually matches your
>>regex. This can result in $1 and $2 containing the values from the last
>>string that worked instead of being blank or failing, which can be hard
>>to track down when you start getting different values than what you
>>expected.
>
>
> Maybe you're used to some old behavior in Perl, but that doesn't
> appear to be the case in 5.8.8 at least. I get this output:
>
> C:\TEMP>testre
> $1 AAAA $2 1111
> $1 $2
Maybe try something more like:
foreach ('AAAA1111', '!!!!!!!') {
if (/^(\D{4})(\d{4})$/) { # if exactly 4 ea alpha/num
print "alpha='$1' num='$2'\n";
} else {
print "Bad expr: $_\n";
}
}
What is the purpose of this illustration in the context of the
original stated problem? The original problem stated that the string
will have different numbers of alphabetic characters and numbers while
your regex specifies 4 digits exactly. My code was in reference to the
"potential problem with this algorithm" statement above where that
person thought that old values for $1 and $2 may carry forward through
match operations, which they do not, as illustrated below.
> from this test script:
>
> use strict;
>
> my $str = 'AAAA1111';
> matchit();
> $str = '!!!!!!!!';
> matchit();
>
> sub matchit
> {
> if (length $str == 8) {
> $str =~ /^(\D+)(\d+)$/;
> print "\$1 $1 \$2 $2\n";
> }
> }
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs