Regular expressions to find the presence of special characters

V

Vertica Garg

I am trying to validate that the string contains characters [A-Z a-z 0-9 /
.. -] only. If there is any other charcter in the string, I have to print the
error message. No special characters like *, &, $ etc should be allowed.

Example:
$code=Hello*Hi

$code=567$fg

$code=@ghyt

For each of the above, I want to print error message.

Is there any easy way of doing it using regular expressions?

Regards,
Vertica
 
J

Jürgen Exner

Vertica said:
I am trying to validate that the string contains characters [A-Z a-z
0-9 / . -] only. If there is any other charcter in the string, I have
to print the error message. No special characters like *, &, $ etc
should be allowed.

Ask the negated question: You got an error condition if your string contain
any character _not_ in that class.

[^A-Z a-z0-9 / . -]

Unfortunately the doc page entry is well hidden way down in "perldoc
perlre":

You can specify a character class, by enclosing a list of characters in
"[]", which will match any one character from the list. If the first
character after the "[" is "^", the class matches any character not in
the list.

jue
 
V

Vertica Garg

Thanks Jue..

But unfortunately, this does not work in all the cases.
e.g., if

$code="@KK.L";
if ($code=~m/[^A-Za-z0-9.]/)
{
print "Found";
}

I expected the program to print "Found" but it does not.

Regards,
Vertica


Jürgen Exner said:
Vertica said:
I am trying to validate that the string contains characters [A-Z a-z
0-9 / . -] only. If there is any other charcter in the string, I have
to print the error message. No special characters like *, &, $ etc
should be allowed.

Ask the negated question: You got an error condition if your string contain
any character _not_ in that class.

[^A-Z a-z0-9 / . -]

Unfortunately the doc page entry is well hidden way down in "perldoc
perlre":

You can specify a character class, by enclosing a list of characters in
"[]", which will match any one character from the list. If the first
character after the "[" is "^", the class matches any character not in
the list.

jue
 
A

Arndt Jonasson

Vertica Garg said:
But unfortunately, this does not work in all the cases.
e.g., if

$code="@KK.L";
if ($code=~m/[^A-Za-z0-9.]/)
{
print "Found";
}

I expected the program to print "Found" but it does not.

This may be platform-dependent, but on my machine, the same thing
happens. You need to output a newline as well. This will work:

$code="@KK.L";
if ($code=~m/[^A-Za-z0-9.]/)
{
print "Found\n";
}
 
G

Gunnar Hjalmarsson

Vertica said:
Thanks Jue..

But unfortunately, this does not work in all the cases.
e.g., if

$code="@KK.L";
if ($code=~m/[^A-Za-z0-9.]/)
{
print "Found";
}

I expected the program to print "Found" but it does not.

Enable strictures and warnings to have Perl help you prevent such mistakes!
 
A

Anno Siegel

Arndt Jonasson said:
Vertica Garg said:
But unfortunately, this does not work in all the cases.
e.g., if

$code="@KK.L";
if ($code=~m/[^A-Za-z0-9.]/)
{
print "Found";
}

I expected the program to print "Found" but it does not.

This may be platform-dependent, but on my machine, the same thing
happens. You need to output a newline as well. This will work:

$code="@KK.L";
if ($code=~m/[^A-Za-z0-9.]/)
{
print "Found\n";
}

Without "strinct" and "warnings", Perl will silently interpolate the
(empty) package variable "@LL", so the result is ".L". That doesn't
match. Try '@LL.L' instead.

Anno
 
J

Jürgen Exner

[Please do not top post!]
[Tried to rearrange text into chronological sequence]
[Please do not blindly fullquote but trim the quotation to the amount needed
to provide context]
Vertica said:
Jürgen Exner said:
Vertica said:
I am trying to validate that the string contains characters [A-Z a-z
0-9 / . -] only. If there is any other charcter in the string, I
have to print the error message. No special characters like *, &, $
etc should be allowed.

Ask the negated question: You got an error condition if your string
contain any character _not_ in that class.

[^A-Z a-z0-9 / . -]

But unfortunately, this does not work in all the cases.
e.g., if

$code="@KK.L";

What does the array @KK contain?

Or did you mean
$code='@KK.L';
or
$code="\@KK.L";

jue
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top