LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 04-27-2009, 05:16 PM   #1
JoeBleaux
LQ Newbie
 
Registered: Mar 2009
Distribution: Slamd64
Posts: 26

Rep: Reputation: 15
Perl: Limiting odd characters?


Can anyone give me a regex that I can use on $username to limit characters to [A-Z][a-z][0-9][@.-_] (including not allowing white space)?
 
Old 04-27-2009, 05:21 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by JoeBleaux View Post
Can anyone give me a regex that I can use on $username to limit characters to [A-Z][a-z][0-9][@.-_] (including not allowing white space)?
What is "to limit" ? Regular expressions either match or don't.

...

Anyway, have you read 'man perlretut' ? Specifically, "Using character classes" ? It's the exact chapter you have to read.
 
Old 04-27-2009, 05:26 PM   #3
JoeBleaux
LQ Newbie
 
Registered: Mar 2009
Distribution: Slamd64
Posts: 26

Original Poster
Rep: Reputation: 15
When I say limit, I was thinking:
Remove all non-[A-Za-z0-9@.-_] characters
What I REALLY want is:
if ($username != [A-Za-z0-9@.-_]){
print "unallowed characters\n";
}
But one that works...
 
Old 04-27-2009, 05:30 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by JoeBleaux View Post
When I say limit, I was thinking:
Remove all non-[A-Za-z0-9@.-_] characters
What I REALLY want is:
if ($username != [A-Za-z0-9@.-_]){
print "unallowed characters\n";
}
But one that works...
Have you ever looked at regular expression matching ?

I.e. 'perldoc perlop', then look for

m/PATTERN/cgimosx
/PATTERN/cgimosx
.

You've made a number of trivial mistakes in your code (I think four), and the suggested man pages have the answers.
 
Old 04-28-2009, 12:36 PM   #5
JoeBleaux
LQ Newbie
 
Registered: Mar 2009
Distribution: Slamd64
Posts: 26

Original Poster
Rep: Reputation: 15
I looked at those pages but, I don't understand Greek. So, let me re-ask...

Can anyone give me a regex that will match non-[A-Za-z0-9@.-_] characters in an IF statement?
 
Old 04-28-2009, 06:13 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by JoeBleaux View Post
I looked at those pages but, I don't understand Greek. So, let me re-ask...

Can anyone give me a regex that will match non-[A-Za-z0-9@.-_] characters in an IF statement?
It's not Greek, it's English.

The answer to your question, and the fix to you most severe mistake, is is looking again into

'perldoc perlop', then looking for

m/PATTERN/cgimosx
/PATTERN/cgimosx

, then paying attention to =~ characters.

Regular expressions are a computer language - like Perl or "C" or Python or Java.

When you are saying "I don't understand Greek", you are actually saying "I do not want to study a language, bring me a solution".

What is required from you is to read and comprehend from in my case line #712 until line #765, i.e. 54 lines of English. I mean line numbers in

perldoc perlop

screen output.

By the way, if after reading line numbers 712 .. 765 you still don't understand something, post here particular questions.

If you do not specifically explain what you do not understand, we do not know what exactly to explain.
 
Old 04-28-2009, 06:24 PM   #7
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by JoeBleaux View Post
I looked at those pages but, I don't understand Greek. So, let me re-ask...

Can anyone give me a regex that will match non-[A-Za-z0-9@.-_] characters in an IF statement?
If you want to play with regexes, you're going to need to learn a little Greek. You might have better luck if you start with perldoc perlrequick. In any case, here's a kind of ham-handed approach:
Code:
#!/usr/bin/env perl
use strict;
use warnings;

my $name;
do {
  print "What's your choice of username:>> ";
  chomp($name = <STDIN>);
  if ($name =~ /[^-A-Za-z0-9@._]/) {
    print "Tsk tsk: bad characters!\n";
  }
} until ($name eq 'quit');
In a nutshell [] creates a character class. It says look for the stuff inside the square brackets. If you use [^], you are doing a negative character class. So it says, look for anything other than what's inside the square brackets. So, I took what you said were the allowed characters, and I said "If you find anything that isn't one of these, scold the user." The only real trick there is that if you want to search for a literal '-' in a character class you need to put it first or escape it (\-). Other than that, it's pretty much straightforward.

Last edited by Telemachos; 04-28-2009 at 06:25 PM.
 
Old 04-29-2009, 09:23 AM   #8
JoeBleaux
LQ Newbie
 
Registered: Mar 2009
Distribution: Slamd64
Posts: 26

Original Poster
Rep: Reputation: 15
Sergei, thank you for your replies but, I am not a programmer and I do not have that mindset. I try and do as little coding as possible.

'RTFM' replies do me no good. I need examples of how things work. I have read Perl docs until my eyes bleed and I do not understand them (same with man pages), they are useless to me. I need examples to understand.

Telemachos reply, "/[^-A-Za-z0-9@._]/" explains it all to me...

// = This is a regex
^ = not the following
-A-Za-z0-9@._ = allowed characters
[] = match the characters inside these.

I understand.

54 lines of Perldoc gibberish just confuses me. It is the same as Greek.
 
Old 04-29-2009, 09:30 AM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I would like to build a house. I don't want to use a hammer or a saw, and I don't want to learn any builder's techniques.
--- rod.
 
Old 04-29-2009, 09:35 AM   #10
JoeBleaux
LQ Newbie
 
Registered: Mar 2009
Distribution: Slamd64
Posts: 26

Original Poster
Rep: Reputation: 15
theNbomr I would like to build a house. I don't want to use a hammer or a saw, and I don't want to learn any builder's techniques.
--- rod.


No, more like: "You want to build a house? Here's a book (with no pictures), figure out how to do dovetail joints by reading 54 lines of text."

If I wanted elitism, I'd post on FreeBSD forums...

Last edited by JoeBleaux; 04-29-2009 at 09:36 AM.
 
Old 04-29-2009, 09:47 AM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by JoeBleaux View Post
theNbomr I would like to build a house. I don't want to use a hammer or a saw, and I don't want to learn any builder's techniques.
--- rod.


No, more like: "You want to build a house? Here's a book (with no pictures), figure out how to do dovetail joints by reading 54 lines of text."

If I wanted elitism, I'd post on FreeBSD forums...
After having read quite a bit about construction methods, I do know that dovetail joints are rarely used in homebuilding. And the point is that if you want to do something that you don't already know, it serves you well to do a little studying. That's how those who you expect to hand you an answer learned it.

Allegedly, you want advice on programming, since 'This forum is for all programming questions' and you put a programming language in the subject line of your post. It isn't elitism, it is on-topic. Specific references to authorative documents seem like appropriate forms of help. The helpful people in this forum are generally loath to simply do your work for you, but are almost universally willing to help you learn for yourself.

Code:
 s/\s+//g
--- rod.
 
Old 04-29-2009, 10:06 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
@joe. why don't you do your best?
 
Old 04-29-2009, 10:28 AM   #13
JoeBleaux
LQ Newbie
 
Registered: Mar 2009
Distribution: Slamd64
Posts: 26

Original Poster
Rep: Reputation: 15
theNbomr - I don't know of one single carpenter that ever learned how to build a house by reading a manual. They all learned by example (watching it being done and apprenticing).

I've already said that I am not a programmer, I do not have the mindset. I do what I must and I learn by example.

RTFM replies are useless and elitist. If I understood manuals, I would have said: "can someone point me to the instructions as to how to..." . I didn't, I said "can someone give me the small bit of code that I couldn't figure out on my own".
 
Old 04-29-2009, 10:40 AM   #14
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by JoeBleaux View Post
I said "can someone give me the small bit of code that I couldn't figure out on my own".
Did you see the small bit of code that I gave you?
--- rod.
 
Old 04-29-2009, 10:48 AM   #15
JoeBleaux
LQ Newbie
 
Registered: Mar 2009
Distribution: Slamd64
Posts: 26

Original Poster
Rep: Reputation: 15
theNbomr: I saw the 'remove whitespace' bit of code you gave me... Thanks.
 
  


Reply



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
How to handle odd characters in sed maker10 Programming 6 07-02-2008 03:15 PM
Parenthesis and other odd characters passed to bash function jakeo25 Programming 2 04-03-2008 10:27 AM
spaces and odd characters in directory name in bash? babag Programming 1 04-02-2008 03:28 PM
Limiting RAM usage in a perl script DanTaylor Programming 2 05-05-2006 02:37 PM
Java JTextArea - limiting amount of characters entered Andy@DP Programming 3 03-10-2004 01:16 AM

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

All times are GMT -5. The time now is 07:19 PM.

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