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 03-16-2007, 12:29 PM   #1
craig467
Member
 
Registered: Jun 2005
Location: Maine
Distribution: Red Hat 9
Posts: 65

Rep: Reputation: 15
Regex Explanation and streamlining


Hi there,

This is similar to a previous post but I need help understanding and streamlining a bit of code. The purpose of the code below is to validate that the user enters a street address in $a using letters, numbers, single spaces and periods. I was trying to write a single matching statement in the if line but could not do it. The only solution I could come up with is what I came up with below.

Code:
if ($a !~ m/^[A-Za-z\d\.\s]+$/ || $a =~ m/\s{2,}/) {
        $error="Y";
	$msg = $msg . "<p>The Address field must contain only letters, numbers";
	$a =~ s/\s/&nbsp\;/g;	#place spaces in string if they are there-for error.
	$msg = $msg . ", single spaces and periods(.) - \"$a\" is invalid.</p>\n";
}  #end if
Is it possible to make it so that you do not have to use the or(||) statement to account for multiple spaces within the string contained by $a. I would have thought that you could have melded the 2 statements in the if line together to account for everything but I could not do it. Can someone please help if it can be done.

I want "123 Barneby Rd." to pass (no error) but I want an error to flag when there is more than one space entered like if there were 2 or 3 spaces between 123 and Barneby like:
Code:
123  Barneby Rd.
123   Barneby Rd.
 
Old 03-17-2007, 12:01 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by craig467
Hi there,

This is similar to a previous post but I need help understanding and streamlining a bit of code. The purpose of the code below is to validate that the user enters a street address in $a using letters, numbers, single spaces and periods. I was trying to write a single matching statement in the if line but could not do it. The only solution I could come up with is what I came up with below.

Code:
if ($a !~ m/^[A-Za-z\d\.\s]+$/ || $a =~ m/\s{2,}/) {
        $error="Y";
	$msg = $msg . "<p>The Address field must contain only letters, numbers";
	$a =~ s/\s/&nbsp\;/g;	#place spaces in string if they are there-for error.
	$msg = $msg . ", single spaces and periods(.) - \"$a\" is invalid.</p>\n";
}  #end if
Is it possible to make it so that you do not have to use the or(||) statement to account for multiple spaces within the string contained by $a. I would have thought that you could have melded the 2 statements in the if line together to account for everything but I could not do it. Can someone please help if it can be done.

I want "123 Barneby Rd." to pass (no error) but I want an error to flag when there is more than one space entered like if there were 2 or 3 spaces between 123 and Barneby like:
Code:
123  Barneby Rd.
123   Barneby Rd.

I would suggest to trim those spaces to 1 space only if user enters more than 1 space, then let it pass. This is not much of a solution to your code problem, but i think this will not annoy the user much if he accidentally keys in 2 or more spaces and could not get pass (while the address actually correct). unless of course that's your strict requirement
 
Old 03-19-2007, 08:26 AM   #3
craig467
Member
 
Registered: Jun 2005
Location: Maine
Distribution: Red Hat 9
Posts: 65

Original Poster
Rep: Reputation: 15
Thanks ghostdog74,

I agree with you but the exercise I was doing wanted me to trap that. Thanks. I am just not sure if the if statement could be shortened into 1 statement. Does anyone know if it could.

Thanks for the input ghostdog74, I agree that it would be more of an annoyance than a help but I am just trying to do what is asked for
 
Old 03-19-2007, 02:47 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,588

Rep: Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894Reputation: 1894
what about:
Code:
 if ($a !~ m/^([A-Za-z\d\.]+\s?)+$/)
 
Old 03-22-2007, 01:32 PM   #5
craig467
Member
 
Registered: Jun 2005
Location: Maine
Distribution: Red Hat 9
Posts: 65

Original Poster
Rep: Reputation: 15
Thanks

Quote:
Originally Posted by ntubski
what about:
Code:
 if ($a !~ m/^([A-Za-z\d\.]+\s?)+$/)
That worked great. Thanks!
 
  


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
Streamlining my notebook pkozub Linux - Laptop and Netbook 5 03-28-2006 04:09 PM
Streamlining my notebook pkozub Linux - Laptop and Netbook 1 03-26-2006 06:16 PM
Better SUDO explanation maginotjr Slackware 9 07-04-2005 02:59 PM
Explanation or some links.. sh0cker Linux - General 2 02-12-2004 11:19 PM
I Could Use An Explanation winger Linux - General 3 04-13-2002 11:32 PM

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

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