how to represent it via REL

H

Huajian Luo

Hi there,

I need to state the following name convention by perl regular
expression.

"valid names are comprised of only alphanumeric characters
plus the characters '-', '_', and '.'. Names must begin
with a letter.

I've tried the following function
sub valid_name{
my $name = $_

if ($name !~ /*[!a-zA-Z0-9_.-]* | [!a-zA-Z]*/) {
print "Name $name is not valid";
} else {
print "MMMMMMMMMMMMMMMMMMMMM";
}
but it doesn't work.

Any hints on t his?
 
J

John W. Krahn

Huajian said:
I need to state the following name convention by perl regular
expression.

"valid names are comprised of only alphanumeric characters
plus the characters '-', '_', and '.'. Names must begin
with a letter.

$name =~ /\A[a-zA-Z][\w.-]*\z/;


John
 
H

Huajian Luo

John W. Krahn said:
Huajian said:
"valid names are comprised of only alphanumeric characters
plus the characters '-', '_', and '.'. Names must begin
with a letter.

$name =~ /\A[a-zA-Z][\w.-]*\z/;

It works, Thanks for the info.
 
H

Huajian Luo

John W. Krahn said:
Huajian said:
I need to state the following name convention by perl regular
expression.

"valid names are comprised of only alphanumeric characters
plus the characters '-', '_', and '.'. Names must begin
with a letter.

$name =~ /\A[a-zA-Z][\w.-]*\z/;
But I have a problem when $name is "s_@_______", I tried qw()
but it still think it's valid.
 
S

Samwyse

Huajian said:
John W. Krahn said:
$name =~ /\A[a-zA-Z][\w.-]*\z/;

But I have a problem when $name is "s_@_______", I tried qw()
but it still think it's valid.

1) How are you using qw()? It is used for array contants.

2) "s_@_______" shouldn't match the reg-ex that John gave you. Are you
sure you copied it correctly?

3) Show us the code that fails.
 
S

Scott Bryce

Huajian said:
John W. Krahn said:
Huajian said:
"valid names are comprised of only alphanumeric characters
plus the characters '-', '_', and '.'. Names must begin
with a letter.

$name =~ /\A[a-zA-Z][\w.-]*\z/;

But I have a problem when $name is "s_@_______", I tried qw()
but it still think it's valid.

Add these two lines at the beginning of your script:

use strict;
use warnings;

That will help you find the problem. I suspect your script won't
compile, but the errors and warnings will help you see the problem.

In case you still can't figure it out, this script demonstrates the problem:


my $name = "s_@_______";

print somesub($name), "\n";

$name = 's_@_______';
print somesub($name), "\n";

sub somesub
{
return shift;
}
 
H

Huajian Luo

Scott Bryce said:
use strict;
use warnings;

That will help you find the problem. I suspect your script won't
compile, but the errors and warnings will help you see the problem.
I just write a test prog to test my function, sorry.
In case you still can't figure it out, this script demonstrates the problem:


my $name = "s_@_______";

print somesub($name), "\n";

$name = 's_@_______';
print somesub($name), "\n";

sub somesub
{
return shift;
}
Yes, you and John's REL are right the reason is that the string I issued,
I should Use single quote instead of double quote, to avoid interpolation.

Thanks for your help.
 
B

Bart Lateur

Huajian said:
"valid names are comprised of only alphanumeric characters
plus the characters '-', '_', and '.'. Names must begin
with a letter.

I've tried the following function
sub valid_name{
my $name = $_

if ($name !~ /*[!a-zA-Z0-9_.-]* | [!a-zA-Z]*/) {
print "Name $name is not valid";
} else {
print "MMMMMMMMMMMMMMMMMMMMM";
}

Let's work with what you've got... First of all, negating a character
class happens with "^", not "!". It looks to me like you want to test if
some characters don't fit the description, then you want it to fail. But
it appears to me you have too many negations. Reduced:

$name =~ /[^a-zA-Z0-9_.\-]|^[^a-zA-Z]/

This will succeed if there's an unacceptable character anywhere, or a
non-letter at the front. And then you have an unacceptable name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top