Sorry, I'm not a native English speaker. Professional Perl programmers doubt that with one regex can validate arithmetic expressions, because of unlimited level of nested parentheses, and they use special Perl modules for this purpose. I wrote such regex. To do this, I used simple and elegant method by which you can write a similar complex expressions. I wrote an article about it, but unfortunately not in English ...
Here's my example:

#!perl -w use strict; use re 'eval'; # Validation of arithmetic expressions like +1*(2-3/(4*5)) # Author: Serge Melnikov (www.cronc.com) my @testexpr=( # Valid arithmetic expressions: '+3', '-(3/4+(-2-3)/3)', '(-((3)))', '-(+1+2)*(3/(1-2))/((-3))', # Invalid (from the point of view of a human or mathematical notation) + arithmetic expressions: '2*-3', # in math we must write 2*(-3) '-(3/4+(-2-3)/3', '(-((3)+))'); my $number=qr/\d+/; # the regex for a term (number/variable/function c +all) my $arithm; # The x modifier used only to split the long regex to publish it $arithm=qr#[+-]?(?:$number|\((??{$arithm})\))(?:[*/](?:$number|\((??{$ +arithm})\)))* (?:[+-](?:$number|\((??{$arithm})\))(?:[*/](?:$number|\((??{$arithm +})\)))*)*#x; print /^$arithm$/ ? "Valid: $_\n" : "Invalid: $_\n" for (@testexpr);

In reply to RFC: Perl regex to validate arithmetic expressions by Serge314

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.