Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parser questions 2

Status
Not open for further replies.

blitzer

Programmer
Joined
Jun 18, 2001
Messages
34
Location
CA
1) How do I make the parser cause the script to die if the information submitted includes characters other than a-zA-Z0-9? When I tryed doing this it only gave an error when the first character wasn't a-z etc.

2) I also want my parser kill the script if the length of the data is greater than say.. 200 characters.


Thanks.
 
First part,
Code:
#!/usr/local/bin/perl
# regex explain - /[^\w\s]/
# the up carret reverses the match behavior for the class
# rather than matching a word char or white space, this matches
# anything other than a word char or white space.

$str = 'some_@_plain_text';
if ($str =~ /[^\w\s]/) { print "Found a non-word char, $&\n"; }
else { print "clean string, => $str\n"; }


$str = 'some_plain_text';
if ($str =~ /[^\w\s]/) { print "Found a non-word char, $&\n"; }
else { print "clean string, => $str\n"; }


# excluding underscores
$str = 'someplaintextanddigits12345';
if ($str =~ /[^a-zA-Z0-9]/) { die "Found a non-word char, $&\n"; }
else { print "clean string, => $str\n"; }

$str = 'someplainte!xtanddigits12345';
if ($str =~ /[^a-zA-Z0-9]/) { die "Found a non-word char, $&\n"; }
else { print "clean string, => $str\n"; }

If you want to 'die' on a non-word char or [^a-zA-Z0-9], then change the
print's above to die's.

2) I also want my parser kill the script if the length of the data is
greater than say.. 200 characters.

if (length($str) > 200 ) { die; }

HTH


keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top