Regular Expression help

X

xsnslaine

Im new to Regular Expressions and have something really really really
simple (i assume to do. but i just cant get the results i want

what i want to do is validate height in feet and inches

for example

5'9 or 5'9" would be acceptable

"/[0-9]\.'?[0-12]/"

that is what have have so far .... but it dont work :(

could someone explain where i have gone wrong? and how to do it?

Thanks in advance.....

Dan.
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g14g2000cwa.googlegroups.com:
Im new to Regular Expressions and have something really really really
simple (i assume to do. but i just cant get the results i want

what i want to do is validate height in feet and inches

for example

5'9 or 5'9" would be acceptable

"/[0-9]\.'?[0-12]/"

Why the \. then?
that is what have have so far .... but it dont work :(

could someone explain where i have gone wrong? and how to do it?

What you have done wrong is to try to use something without
understanding it. From perldoc perlreref:

[...] Matches any one of the characters contained within the brackets

So, the second character class above says match 0, 1 or 2.

It might be preferable in this case to first capture the potential feet
and inches and then do a validity check following a successful match.
Keeps the regex simple.

use strict;
use warnings;

my @s = (q{5'9"}, q{5'9});

for (@s) {
if( /(\d+)'(\d+)"?/ ) {
my $feet = $1;
my $inches = $2;
# bounds check
print "$feet feet $inches inches\n";
}
}


__END__
 
J

Jürgen Exner

Im new to Regular Expressions and have something really really really
simple (i assume to do. but i just cant get the results i want

what i want to do is validate height in feet and inches

for example

5'9 or 5'9" would be acceptable

"/[0-9]\.'?[0-12]/"

Ok, trying to translate back into plain English. This should match

- a single digit between 0 and 9 (i.e. any digit, you could have use \d
instead for better readability)
- followed by a literal dot
- followed by at most one apostroph
- followed by a single digit between 0 and 1 or the digit 2 (that would have
been easier to write as [012] or [0-2])
that is what have have so far .... but it dont work :(
could someone explain where i have gone wrong? and how to do it?

I don't know for sure what you mean by "it don't work" but at least the
literal dot in the RE looks kind of suspicious.
And I don't know either what you consider a valid height versus an invalid
height. Therefore I cannot tell how to fix it.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,167
Messages
2,570,913
Members
47,455
Latest member
Delilah Code

Latest Threads

Top