[LON-CAPA-users] regular expression

Guy Albertelli II lon-capa-users@mail.lon-capa.org
Tue, 16 Aug 2005 10:40:21 -0400 (EDT)


Hi Hon-Kie,

> Is there a way to construct the following regular expression? I want the 
> students to enter number1/(number2 * x) where number1 and number2 are known 
> integers. However, number2 can be 1, so that means they can enter as 
> number1/x and also, number1/(x) or number1/(1*x). Question: For the last 2 
> answers, what I like to check is, if they type the left parenthesis '(' 
> then they need to enter the right parenthesis ')'. I look in the Mastering 
> Regular Expressions book but could not find any info on how to check for an 
> character.

this example also allows number1/(x*number2)

if ($num2 == 1) {
   $re= '/^ \s* \Q$num1\E \s* \/ \s*
           (
             \( \s* 1 \s* \* \s* x \s* \)
           | \( \s* x \s* \* \s* 1 \s* \)
           | \( \s* x \s* \)
           |  x
           )\s*$/x'
} else {
   $re= '/^ \s* \Q$num1\E \s* \/ \s*
           (
             \( \s* \Q$num2\E \s* \* \s*     x     \s* \)
           | \( \s*     x     \s* \* \s* \Q$num2\E \s* \)
           )\s*$/x'
}

-- 
guy@albertelli.com   0-7-2-3-27,137