Re: interpoliation within regexp
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 28 Sep 2006 03:45:47 -0700
Derek B. Smith wrote:
I need to substitute a conversion using chr, but have
failed on multiple attempts. Basically if the first
element contains a # then convert it.
I think you meant "if the first element starts with a digit"? A # is
quite different. Please be as precise as possible.
Will anyone advise?
#if first char is a-z then print it else warn
This comment does not match the code. You are not checking for a-z,
you're checking for non-digits. That includes underscores, whitespace,
capital letters, punctuation, and non-printable characters.
#chop string into individual characters
my @chars = unpack
("A1" x length($password),$password);
if ($chars[0] =~ /^\D/) {
print "Your new string is:\t",@chars,"\n";
}
else {
print "string starts with number, now converting\n", @chars, "\n";
substr($chars[0],0,1) =~ s/\Q{$chars[0]}/{chr($chars[0]}\E/;
what's with all the brackets? Are you aware that you're specifically
searching for actual { and } characters?
Are you aware that the regexp ends at the second /, and that the \E
therefore is meaningless in the replacement?
Are you aware that the replacement is a double-quoted string, not Perl
code, unless you add the /e modifier?
I also have no idea why you're using substr. If you want to only
change one character, change only one character.
$chars[0] =~ s/^(\d)/chr $1/e;
Hope this helps,
Paul Lalli
.
- References:
- interpoliation within regexp
- From: Derek B. Smith
- interpoliation within regexp
- Prev by Date: Re: please advise help with regex
- Next by Date: Re: [Perl Win32]PPM: about the blocking ports or sth
- Previous by thread: interpoliation within regexp
- Next by thread: Re: interpoliation within regexp
- Index(es):
Relevant Pages
|
|