🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.os.linux.programacion
Article #1505

expresion regular para comprobar Expresion matematicas

#1505
From: F
Date: Sat, 08 Oct 2005 10:29
27 lines
1647 bytes
Hola, necesito comprobar que una expresion matematica este bien formada.

por ejemplo 1+2+3 esta bien 1*/3 no

Para ello, cuando no hay parentesis es bastante facil


$num="-?\\d*\\.?\\d+";
$op='\*\/\%\+\-';

$_="12+1 * 4/2";
if (
/^$num\s*([$op]\s*$num\s*)*$/
){
  print "Esta bien\n";
}

Pero cuando hay parentesis, lo unico que se ocurre, es hacer una funcion 
recursiva, que compruebe  cada vez que hay un parentesis, lo de dentro esta 
bien formado.

Resumiendo, no se hacer que la expresion regular permita parentesis dentro 
de parentesis, y demas, me imagino que esto es posible

Alguna idea?

Gracias
Technical Details
Message-ID: <ccN1f.4441850$I96.7525036@telenews.teleline.es>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news2.volia.net!fu-berlin.de!nsnmpen1-lo.nuria.telefonica-data.net!telenews.teleline.es!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: f86c8,9615da75e63f6370,start
X-Google-Attributes: gidf86c8,public
X-Google-Language: SPANISH,ASCII-7-bit
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news2.volia.net!fu-berlin.de!nsnmpen1-lo.nuria.telefonica-data.net!telenews.teleline.es!not-for-mail
X-Trace-PostClient-IP: 82.159.102.16
From: "F" <esnewsfQuItAeStO@EsToCaMbIeNyahoo.es>
Newsgroups: es.comp.os.linux.programacion
Subject: expresion regular para comprobar Expresion matematicas
Lines: 30
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
Message-ID: <ccN1f.4441850$I96.7525036@telenews.teleline.es>
Date: Sat, 08 Oct 2005 10:29:28 GMT
NNTP-Posting-Host: 10.20.31.4
X-Complaints-To: usenet@teleline.es
X-Trace: telenews.teleline.es 1128767368 10.20.31.4 (Sat, 08 Oct 2005 12:29:28 MET DST)
NNTP-Posting-Date: Sat, 08 Oct 2005 12:29:28 MET DST
Organization: Terra Networks