LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-16-2006, 05:37 PM   #1
richikiki
Member
 
Registered: Mar 2005
Location: Montréal, Québec, Canada
Distribution: Slackware 12.1 x32, 13.1 x64
Posts: 90

Rep: Reputation: 15
Question perl regular expression a char match


Hi guys,

I want how to validate a string line searching for any character which is not defined as:
PHP Code:
from a to z
from A to Z
from 0 to 9
. (point
- (-)
"\" (back slash) finally
[white space] 
In this case I'm looking in the whole string line give me a error message if find any char which is not in my list. I'm using this:

Code:
#!/usr/bin/perl
...
...

   if( $mystring !~ m/[a-zA-Z_0-9\.\-\\]|[ ]|/ ) # if $mystring has a non-defined char
   {
       print "Error";
   }
Does any one know what im doing wrong?
I guess my regular expressions

Regards
 
Old 07-16-2006, 10:08 PM   #2
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
well it looks close.. why is there an underscore in there?

it might work like this .. maybe..
[a-zA-Z0-9\.\-\\ ]
 
Old 07-16-2006, 10:51 PM   #3
koobi
Member
 
Registered: Jun 2006
Location: Colombo, Sri Lanka
Distribution: Ubuntu
Posts: 103

Rep: Reputation: 15
this should work. you dont have to escape a hyphen if it's the first char
Code:
/[-a-zA-Z0-9\.\\\ ]+/
also, if there's a modifier to make it case insensitive, you can exclude either the a-z or A-Z
Code:
/[-a-z0-9\.\\\ ]+/i

if you want those chars to appear in that exact same order, the expression has to be changed but i'm not sure if that's what you want.

Last edited by koobi; 07-16-2006 at 10:53 PM.
 
Old 07-16-2006, 10:53 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
I wonder if you would might use [\w\s]+ instead.
For example:
Code:
#!/usr/bin/perl
# Example: ./myscript This is \^ test!
# Example: ./myscript There aren\'t \~ 33 Numbers \ HEre.

foreach $mystring (@ARGV) {
   if( $mystring !~ m/([\w\s]+)/ ) # if $mystring has a non-defined char
   {
       print "$mystring is not defined\n";
   }
}
 
Old 07-16-2006, 10:57 PM   #5
koobi
Member
 
Registered: Jun 2006
Location: Colombo, Sri Lanka
Distribution: Ubuntu
Posts: 103

Rep: Reputation: 15
Quote:
Originally Posted by homey
I wonder if you would might use [\w\s]+ instead.
For example:
Code:
#!/usr/bin/perl
# Example: ./myscript This is \^ test!
# Example: ./myscript There aren\'t \~ 33 Numbers \ HEre.

foreach $mystring (@ARGV) {
   if( $mystring !~ m/([\w\s]+)/ ) # if $mystring has a non-defined char
   {
       print "$mystring is not defined\n";
   }
}

but \w includes an underscore as well, doesn't it?
 
Old 07-17-2006, 12:32 AM   #6
richikiki
Member
 
Registered: Mar 2005
Location: Montréal, Québec, Canada
Distribution: Slackware 12.1 x32, 13.1 x64
Posts: 90

Original Poster
Rep: Reputation: 15
Unhappy

Thanks guys,

but still not working

I try this:

Code:
$mystring = "!20060710,NONE,NONE,GROUP_INC,Swap,Senior Unsecured,CDS SDA,NONE,100y,0.12,0.12"
it has "!", ",". which are suppouse to not be allow on my check function. Im using this:

Code:
foreach $mystring (@ARGV)
   {
       	if( $mystring !~ m/[a-zA-Z_0-9\-\.\\ ]+/i )
         {
             print "\nError\n";
         }
   }
Nothing happen I dont know why the regular expression seems ok.
Maybe something with m/expr/option. the option are i, g, e but not sure which one is the right one, I try all them.

I'm allowing the next chars:
PHP Code:
From a to z
From A to Z
"_" [undescore]
From 0 to 9
"-" [hypen] or is this [dash]?
"." (point
"\" [back slash] and
" " [white space] 
 
Old 07-18-2006, 08:01 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
your logic is wrong.
!~ is the NOT of =~
you are finding if NOT ANY legal chars are there.
you need a positive search for illegal chars (think about it)

Code:
          if $mystring =~ /[^-.A-Za-z0-9\s]/ ;

Last edited by bigearsbilly; 07-18-2006 at 08:03 AM.
 
Old 07-18-2006, 09:08 PM   #8
richikiki
Member
 
Registered: Mar 2005
Location: Montréal, Québec, Canada
Distribution: Slackware 12.1 x32, 13.1 x64
Posts: 90

Original Poster
Rep: Reputation: 15
Lightbulb

Thanks bigearsbilly,

At the end I went to search for the chars that I dont want because my logic seems wrong.
I did the next then:

Code:
if( $mystring =~ m/[!"#\$\%&'()\*\+\,\-\/:<=>\?\@\[\\\]\^\`\{\|\}\~]+/g )# ch
ar ";" is valid after sustitution of "," for ";".
   {
        print "Error on line: $i a invalid char found aborting the parsing\n";
   }
I guess the above is different for what I was doing before, the negation of the regular expression.
All this because it could contained corrupted data which come from the excel file.Thats way I'm looking for any weird char.

I don't thing it could get control char data from the excel file isnt .
Thanks everybody for your comments.
I love this site.
 
Old 07-19-2006, 03:37 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by richikiki
At the end I went to search for the chars that I dont want because my logic seems wrong.
I think it's generally agreed better to have a white list rather than a black list.
i.e. I think the first match is better as you never know what you've missed
with the second method.

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl regular expression problem true_atlantis Programming 4 05-27-2009 06:35 AM
Having trouble with a perl regular expression... jayemef Programming 3 08-25-2005 11:00 PM
Regular expression to match a valid URL string vharishankar Programming 13 07-21-2005 09:17 PM
Perl Regular Expression dilemma GATTACA Programming 1 03-27-2004 07:48 PM
Perl Regular Expression rch Programming 14 07-11-2003 11:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration