CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Blocking input of specific characters

 



tbone587
User

Oct 11, 2011, 9:16 PM

Post #1 of 12 (2188 views)
Blocking input of specific characters Can't Post

What would be the easiest way to block input of essentially all characters but - . _ ? I tried the code below, but it throws a lot of errors. I used the \W operator now, but the downfall for me is that it blocks dashes.


Code
elsif($Host_Name =~ m/[`~!@#$%^&*()=+[{]};:'",<>\/?\\|]]/)



rovf
Veteran

Oct 12, 2011, 3:26 AM

Post #2 of 12 (2185 views)
Re: [tbone587] Blocking input of specific characters [In reply to] Can't Post

Instead of saying "a lot of errors", you should post *what* errors you get.


tbone587
User

Oct 12, 2011, 5:17 AM

Post #3 of 12 (2182 views)
Re: [rovf] Blocking input of specific characters [In reply to] Can't Post

Backslash found where operator expected at ./removeHost.pl line 61, near "m/[`~!@#$%^&*()=+,<>/\"
syntax error at ./removeHost.pl line 61, near "m/[`~!@#$%^&*()=+,<>/\"
Search pattern not terminated at ./removeHost.pl line 61.


rovf
Veteran

Oct 12, 2011, 6:56 AM

Post #4 of 12 (2180 views)
Re: [tbone587] Blocking input of specific characters [In reply to] Can't Post


Quote
m/[`~!@#$%^&*()=+,<>/\


Please provide more context. I tried your original regexp,


Code
use strict; use warnings; 
'xxx' =~ m/[`~!@#$%^&*()=+[{]};:'",<>\/?\\|]]/


and it executed without error.


tbone587
User

Oct 12, 2011, 9:11 AM

Post #5 of 12 (2176 views)
Re: [rovf] Blocking input of specific characters [In reply to] Can't Post

I have just wrote a quick script on the bare bones script on what I trying to do. It now works when I broke it down, but its detected numbers as an invalid character as well now.


Code
#!/usr/bin/perl 


my $Host_Name;


print "Please enter the host name:";
chomp ($Host_Name=<STDIN>);


if ($Host_Name =~ Tristan)
{
print "Hello Tristan\n";
exit;
}

elsif($Host_Name =~ m/[`~!@#$%^&*()-=+,<.>?;|:'"]/ || $Host_Name =~ m/[{}]/ || $Host_Name =~ m/[[]/ || $Host_Name =~ m/[]]/)
{

print "The following invalid character has been entered: $Host_Name\n";
exit;
}

else
{ print "Something else has been entered\n";

}



FishMonger
Veteran / Moderator

Oct 12, 2011, 9:16 AM

Post #6 of 12 (2175 views)
Re: [tbone587] Blocking input of specific characters [In reply to] Can't Post

Why are you attempting to use a character class inside a character class?

You say you want to block everything except - . _ but your regex doesn't even come close to doing what your want.

Please provide more details on exactly what you need to accomplish.


tbone587
User

Oct 12, 2011, 9:34 AM

Post #7 of 12 (2172 views)
Re: [FishMonger] Blocking input of specific characters [In reply to] Can't Post

I dont want the user to be able to enter any special characters, except for _'s and -'s. So I tried to make that long regex with every character that I dont want it to accept.


FishMonger
Veteran / Moderator

Oct 12, 2011, 9:43 AM

Post #8 of 12 (2169 views)
Re: [tbone587] Blocking input of specific characters [In reply to] Can't Post

So, you only want to allow a..zA..Z0..9-_.
Is that correct?


Code
if ( $Host_Name =~ /^[\w-.]+$/ ) { 
# only contains valid characters
}
else {
# contains invalid characters
}



(This post was edited by FishMonger on Oct 12, 2011, 9:44 AM)


tbone587
User

Oct 12, 2011, 10:08 AM

Post #9 of 12 (2165 views)
Re: [FishMonger] Blocking input of specific characters [In reply to] Can't Post

Yes that is correct. Can you explain to be what the +$ means? I understand everything else. I really appreciate it.


BillKSmith
Veteran

Oct 12, 2011, 10:08 AM

Post #10 of 12 (2165 views)
Re: [tbone587] Blocking input of specific characters [In reply to] Can't Post

The hyphen has special meaning in a character class. (Search for 'range' in perldoc perlre). FishMonger's approach is much easier. Note that his use of the hyphen is correct, it is a special case.
Good Luck,
Bill


tbone587
User

Oct 12, 2011, 10:32 AM

Post #11 of 12 (2160 views)
Re: [BillKSmith] Blocking input of specific characters [In reply to] Can't Post

What does the +$ mean?


FishMonger
Veteran / Moderator

Oct 12, 2011, 10:43 AM

Post #12 of 12 (2159 views)
Re: [tbone587] Blocking input of specific characters [In reply to] Can't Post

See "Anchors" and "Quantifiers" in: perldoc perlreref - http://perldoc.perl.org/perlreref.html#ANCHORS


(This post was edited by FishMonger on Oct 12, 2011, 10:43 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives