A Perl regexp to validate arithmetic expressions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A Perl regexp to validate arithmetic expressions
# 1  
Old 12-06-2010
A Perl regexp to validate arithmetic expressions

Can a Perl regexp validate arithmetic expressions? I say yes. Here is my Perl regexp to validate arifmetic expressions:
Code:
#!perl -w
use strict;
use re 'eval';
 
my @testexpr=(
# Valid arifmetic expressions
'+3',
'-(3/4+(-2-3)/3)',
'(-((3)))',
'-(+1+2)*(3/(1-2))/((-3))',
# Invalid (from the point of view of a human) arifmetic expressions
'2*-3',
'-(3/4+(-2-3)/3',
'(-((3)+))');
 
my $number=qr/\d+/; # pattern for a digit/variable
my $arifm;
# The modifier x is used to split the regexp to publish it in Web
$arifm=qr#[+-]?(?:$number|\((??{$arifm})\))(?:[*/](?:$number|\((??{$arifm})\)))*
   (?:[+-](?:$number|\((??{$arifm})\))(?:[*/](?:$number|\((??{$arifm})\)))*)*#x;
 
print /^$arifm$/ ? "Valid: $_\n" : "Invalid: $_\n" for (@testexpr);

Serge

Moderator's Comments:
Mod Comment Removed Self-Promoting Link Spam
# 2  
Old 12-06-2010
# Valid arifmetic expressions
'+3',
'-(3/4+(-2-3)/3)',
'(-((3)))',
'-(+1+2)*(3/(1-2))/((-3))',
'2*-3',
# Invalid (from the point of view of a human) arifmetic expressions
'-(3/4+(-2-3)/3',
'(-((3)+))');
# 3  
Old 12-07-2010
Yes, 2*-3 is valid for Perl, Delphi and other compilers, but in mathematic we must write 2*(-3).

Serge
# 4  
Old 12-07-2010
This sort validation is more a bison/lex/flex sort of thing, probably available in PERL, too. A small state machine in C could apply the rules, too, tracking parse state. However, I suspect even the most talented regex writer cannot decode infinitely complex state in a regex. Some things are easier done in stages, too, like parentheses and quote balance. Each milieu comes with it's own rules about what is an escape, how linefeeds are treated, punctuation of just white space.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl regexp to extract first and second column

Hi, I am trying with the below Perl one-liner using regular expression to extract the first and second column of a text file: perl -p -e "s/\s*(\w+).*/$1/" perl -p -e "s/\s*.+\s(.+)\s*/$1\n/" whereas the text file's data looks like: Error: terminated 2233 Warning: reboot 3434 Warning:... (3 Replies)
Discussion started by: royalibrahim
3 Replies

2. Shell Programming and Scripting

Perl regexp help

Hi, I have file like below: 1|1212|34353|5fdf 6575||dfgdg sfsdf |afsf||4|aasfbc|~1213~~~~~ 1|1212|34353|5fdf 6575||dfgdg sfsdf |affsf| |4|abc|~rwarw~~asa~~~123~312313 1|1212|34353|5fdf 6575||dfgdg sfsdf |afasfs||4|aasfdbc|~564564~~~~ 1|1212|34353|5fdf 6575||dfgdg sfsdf... (1 Reply)
Discussion started by: sol_nov
1 Replies

3. Shell Programming and Scripting

PERL Regular Expressions

im trying to extract some tags between and in a file..for eg..the file format is I want the and extracted from the file i.e the tags which is present b/w and I have the regex for extracting the tags from the whole file but how to specify my search within the and... (1 Reply)
Discussion started by: rajkrishna89
1 Replies

4. Shell Programming and Scripting

perl regexp: no match across newlines

Hi. Here's a tricky one (at least to me): I have a file named theFile.txt (UTF-8) that contains the following: a b cWhen I execute perl -pe 's|a.*c|d|sg' theFile.txtin bash 3.2 on MAC OS X 10.6, I get no match, i.e. the result is a b cagain. Any clues why? (2 Replies)
Discussion started by: BatManWSL
2 Replies

5. Shell Programming and Scripting

perl regexp matching

Hello, I cannot see what's wrong in my code. When I run code below, it just print an empty string. my $test = "SWER~~ERTGSDFGTHAS_RTAWGA_DFAS.x4-234253454.in"; if ($test = ~ m/\~{1,2}.*4/) { print "$1\n"; } else { print "No match...\n"; } Anyone know what I'm doing wrong? ... (4 Replies)
Discussion started by: urandom
4 Replies

6. Shell Programming and Scripting

Regular expressions - Perl

Hello everybody, I am trying to connect from hp-ux to win 2003 using perl's Net::Telnet module. Seeing the examples in couple of web sites, I saw I have to declare a Prompt => Can somebody please tell me what my regular expression should be? The prompt after I log in is: ... login:... (1 Reply)
Discussion started by: whatever
1 Replies

7. UNIX for Dummies Questions & Answers

Regular Expressions HELP - PERL

Hello, $line=USING (FILE '/TEST1/FILENAME'5000) I want to reterive the value between ' and ) which is 5000 here. i have tried out the following expressions ... Type 1 : $Var1=`sed -e 's/.*\' //' -e 's\).*$/' $line`; Type 2 : $Var1=`echo $line | awk -F"\'" '{print $2}' | awk -F"\\)"... (1 Reply)
Discussion started by: maxmave
1 Replies

8. Shell Programming and Scripting

perl regexp

What is the easiest way to get full address of *.jpg images from html file using perl? example: http://farm3.static.flickr.com/2397/2126443111_65a810004c.jpg (1 Reply)
Discussion started by: mirusnet
1 Replies

9. Shell Programming and Scripting

Perl regular expressions...

I am writing script that will act like the 'comm' utility. My problem is when trying to read whether the user has entered -123 or -1 or -1...etc. I currently have: if(m/??/g){ print "Good.\n"; } So, this should check for all... (1 Reply)
Discussion started by: DrRo183
1 Replies

10. Shell Programming and Scripting

Need Help with Perl REGEXP

I need help with a Perl regular expression. The following string blows up my program: <david(greg jim)> If I type this string, there is no problem: <david(greg_jim)> or type david(gregjim) or type <david greg jim> the CGI program does not complain. For some reason that I do not understand the... (1 Reply)
Discussion started by: mh53j_fe
1 Replies
Login or Register to Ask a Question