2 Replies - 1144 Views - Last Post: 02 May 2009 - 07:15 AM

#1 Ed_Bighead   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 178
  • Joined: 26-April 09

Hopefully quick problem with regular expression

Posted 29 April 2009 - 04:11 PM

So this code is supposed to check input to see if it matches something like this: "C:\folder\file.exe"
sub addProg {
	my $expr = "m/^[a-zA-Z]:\\.*\.exe$/";
	print "\n\nPlease enter the location of the executable.\n"
		."For example:\n\tC:\\Program Files\\Microsoft Games\\Solitaire\\Solitaire.exe\n\n"
		."Program: ";

	until($input =~ $expr) {
		$input = <>;
		chomp($input);
		if(!($input =~ $expr)) {
			print "\nLocation must proper format.\n";
		}
	}
	return;
}

What I thought the expression checked for was
1. Starts with a letter
2. followed by colon
3. followed by '\'
4. followed by any number of anything
5. ends with '.exe'

When I enter something like 'C:\program.exe', it doesn't match. Any thoughts?

Is This A Good Question/Topic? 0
  • +

Replies To: Hopefully quick problem with regular expression

#2 Ed_Bighead   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 178
  • Joined: 26-April 09

Re: Hopefully quick problem with regular expression

Posted 29 April 2009 - 06:43 PM

Found the problem. Either you can't assign regular expressions to a variable or I'm doing it wrong. This is the working code, I just substituted the actual expression where i thought I could use the variable.
sub addProg {
	print "\n\nPlease enter the location of the executable.\n"
		."For example:\n\tC:\\Program Files\\Microsoft Games\\Solitaire\\Solitaire.exe\n\n";

	until($input =~ /^[a-zA-Z]:\\.+\.exe$/) {
		print "Program: ";
		$input = <>;
		chomp($input);
		if(!($input =~ m/^[a-zA-Z]:\\.+\.exe$/)) {
			print "\nLocation must be in proper format.\n";
		}
	}
	return;
}


The reason I thought you could so something like this was because, in php, you can do
$regEmail="^[a-zA-Z0-9._-][a-zA-Z0-9._-]*@[a-zA-Z0-9_-]+\.+[a-zA-z]{2,}$";
if (!preg_match("/".$regEmail."/",$email)) {
				  echo "The e-mail you entered is not a valid e-mail. Please go back and try again.";

Was This Post Helpful? 0
  • +
  • -

#3 dsherohman   User is offline

  • Perl Parson
  • member icon

Reputation: 227
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Hopefully quick problem with regular expression

Posted 02 May 2009 - 07:15 AM

View PostEd_Bighead, on 30 Apr, 2009 - 01:43 AM, said:

Either you can't assign regular expressions to a variable or I'm doing it wrong.

You can assign a regex to a variable, you just ran into some quoting issues. You could have used qr (quote regex) to get it to handle quoting in a more appropriate fashion for a regular expression, or you could have used a print statement to verify that $expr was being set to what you wanted so that you could adjust your quoting accordingly.

Also note that the expression you wanted to test against is just
^[a-zA-Z]:\\.*\.exe$
m// is an operator, not part of the regex itself.

#!/usr/bin/perl

use strict;
use warnings;

addProg();
exit;

sub addProg {
  my $expr = qr/^[a-zA-Z]:\\.*\.exe$/;
  # or
  # my $expr = "^[a-zA-Z]:\\\\.*\.exe\$";
  # or
  # my $expr = '^[a-zA-Z]:\\\\.*\.exe$';
  print "\n\nPlease enter the location of the executable.\n"
	."For example:\n\tC:\\Program Files\\Microsoft Games\\Solitaire\\Solitaire.exe\n\n"
	."Program: ";

  while (1) {
	my $input = <>;
	chomp($input);
	if(!($input =~ m/$expr/)) {
	  print "\nLocation must proper format.\n";
	} else {
	  last;
	}
  }
  return;
}


(I also adjusted the loop structure a little to a: only test each $input against $expr once and b: run cleanly with "use warnings" enabled.)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1