LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-25-2005, 11:04 AM   #1
jayemef
Member
 
Registered: Aug 2005
Location: Juniata College, PA
Distribution: Ubuntu, Slackware
Posts: 67

Rep: Reputation: 15
Having trouble with a perl regular expression...


I'm trying to write a regular expression in perl to pull student usernames from input. Our campus usernames consist of the first five letters of our last name, followed by first initial, followed by middle initial (x if the person doesn't have one), followed a number representing the year they started. This number used to be single digit, but new ones are now 2. So as an example, my username is fitzpjm3.

Some things to consider here:
- some last names don't have more than 5 letters, so usernames will vary in length
- there is an optional 0 at the end of the username (like in robbiet04)

The expression I have been building on looks like
Code:
m/^([a-z]*0?[0-9])$/
This searches, from beginning of string to end, for any number of letters, followed by a digit that may or may not have a leading 0. The only trouble I am having is that this matches pure digits. So I get a positive return on a username, but a digit will come back as true as well. How do I get it to only match when everything is true?
 
Old 08-25-2005, 12:47 PM   #2
jayemef
Member
 
Registered: Aug 2005
Location: Juniata College, PA
Distribution: Ubuntu, Slackware
Posts: 67

Original Poster
Rep: Reputation: 15
Well, I was able to resolve the issue by testing to see if $1 (the first part of the expression) exists. By doing it this way, if it finds a digit without letters before it, it won't recognize it as a username.

For example:
Code:
print "FOUND MATCH" if /([A-z]*)(0?[0-9])/ and $1;
This seems a bit obtuse though. If there is a better alternative, please share.

Last edited by jayemef; 08-25-2005 at 12:51 PM.
 
Old 08-25-2005, 08:25 PM   #3
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
The reason it is returning a match on pure digits has to do with the * quantifier on the first part. [A-Za-z]* will match zero or more letters, while [A-Za-z]+ will match one or more letters. Changing the * to a + should fix it. I find it somewhat rare to use *, because it always matches.

Also, beware of [A-z], you are including some non-letters in there (check out an ASCII chart to see what I mean).

Last edited by puffinman; 08-25-2005 at 08:26 PM.
 
Old 08-25-2005, 11:00 PM   #4
jayemef
Member
 
Registered: Aug 2005
Location: Juniata College, PA
Distribution: Ubuntu, Slackware
Posts: 67

Original Poster
Rep: Reputation: 15
Ah, yes, it worked. I totally overlooked the fact that * returns even on 0 matches. As for the A-z, thanks. I had done that on purpose thinking Z was followed by a. It was specifically because \w includes special characters that I chose to approach it that way. Been a while since I had to work with ASCII...

Last edited by jayemef; 08-25-2005 at 11:01 PM.
 
  


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
Perl regular expression issue zikhermm Programming 7 09-23-2005 03:48 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
using a perl regular expression in php markus1982 Programming 5 11-18-2002 02:31 PM

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

All times are GMT -5. The time now is 08:02 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