Regular Expressions 101

Community Patterns

Configurable math parser

1

Regular Expression
PCRE (PHP <7.3)

/
# SETTINGS (?<ANCHORED>){1} (?<NO_SIGN_DUP>){1} (?<PYTHON_POW>){1} (?<COMMENTS>){0} (?(ANCHORED)^(?&s)*) (?(DEFINE) (?<s>(?(COMMENTS)(?:\s+|\/\*[\s\S]*?\*\/|\/\/.*|\#.*)|\s+)) (?<sign>(?(NO_SIGN_DUP)(?<=(?<_>[\s\S]))(?!(?P=_)))[-+]) (?<base>\d+(?:\.\d*)?|\d*\.\d+) (?<exp>(?:[eE][+-]?\d+)) (?<num>(?&sign)?(?&base)(?&exp)?(?!\w)) (?<bracketed>\((?&s)*(?&expr)(?&s)*\)) (?<atom>(?&num)|(?&bracketed)) (?<pow>(?&atom)(?:(?&s)*(?(PYTHON_POW)(?:\^|\*\*)|\^)(?&s)*(?&pow))*) (?<mul>(?&pow)(?:(?&s)*[*\/](?&s)*(?&mul))*) (?<add>(?&mul)(?:(?&s)*[+-](?&s)*(?&add))*) (?<expr>(?&add)) ) (?&expr) (?(ANCHORED)(?&s)*$)
/
gxJm

Description

A validator/parser for math expressions. Written on PCRE regex. To change settings, change the 0 to 1 or 1 to 0 after a setting, example: (?<ANCHORED>){1} to disable, change to (?<ANCHORED>){0}

Submitted by Mateon1 - 9 years ago