473,566 Members | 2,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex to recognize math/string functions


Hi,

Thanks to Peter, Chris and Steven who answered my previous answer about
regex to split a string. Actually, it was as easy as create a regex with the
pattern "/*-+()," and most of my string was splitted.
I am fascinated to the powerfull use of this RegEx class, so I wonder if it
could go a step further.

As a question, can regex be used to valid a set of different functions ?
Example : Suppose I have to verify the correctness of an input string, which
may contains one or more of the following functions :

Round ( NumericValue, Decimals)
Lower( StringValue )
Upper( StringValue )
Abs(NumericValu e)

.... it will be like 15 functions, but let's name just this three.

Note : I just want to validate the input, I don't pretend to perform the
resolving part of this functions, just validate the input in terms of :
1.- Data type of parameters.
2.- Pairing parenthesis.
(the resolution of the of the functions will be done by 3rd party's code).
So, if I receive :
Abs("VB is great").

I would reject that sentense due the characters between parenthesis are a
string, not numeric values.

But, instead if I receive :
Upper( "C# is the best thing since sliced bread")

I would accept the sentence because the parameter is of the proper type.

Also:
Round( 1234.56, 2

would be invalid, due the missing parenthesis.

Finally, the functions can be nested.
So, the question is : can Regex handle this ? or should I start to go for
the parsers libraries ?
Thanks in advance,


Nov 15 '05 #1
2 3413
Hi Tim,

I think you COULD use RegExp to perform such a validation, but there are
more suitable tools for such tasks - lexical analyzers. These are state
machines controlled by so called syntax graphs describing what is valid for
the grammar and what is not. I suppose RegExp uses a similar engine behind
the scenes by building a syntax graph from the regular expression you
provide, but it's just the expression can grow enormously for complex
grammars.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Tim Conner" <ti*******@hotm ail.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..

Hi,

Thanks to Peter, Chris and Steven who answered my previous answer about
regex to split a string. Actually, it was as easy as create a regex with the pattern "/*-+()," and most of my string was splitted.
I am fascinated to the powerfull use of this RegEx class, so I wonder if it could go a step further.

As a question, can regex be used to valid a set of different functions ?
Example : Suppose I have to verify the correctness of an input string, which may contains one or more of the following functions :

Round ( NumericValue, Decimals)
Lower( StringValue )
Upper( StringValue )
Abs(NumericValu e)

... it will be like 15 functions, but let's name just this three.

Note : I just want to validate the input, I don't pretend to perform the
resolving part of this functions, just validate the input in terms of :
1.- Data type of parameters.
2.- Pairing parenthesis.
(the resolution of the of the functions will be done by 3rd party's code).
So, if I receive :
Abs("VB is great").

I would reject that sentense due the characters between parenthesis are a
string, not numeric values.

But, instead if I receive :
Upper( "C# is the best thing since sliced bread")

I would accept the sentence because the parameter is of the proper type.

Also:
Round( 1234.56, 2

would be invalid, due the missing parenthesis.

Finally, the functions can be nested.
So, the question is : can Regex handle this ? or should I start to go for
the parsers libraries ?
Thanks in advance,



Nov 15 '05 #2
"Tim Conner" <ti*******@hotm ail.com> wrote in
news:#K******** ******@TK2MSFTN GP11.phx.gbl:

Hi,

Thanks to Peter, Chris and Steven who answered my previous
answer about regex to split a string. Actually, it was as easy
as create a regex with the pattern "/*-+()," and most of my
string was splitted. I am fascinated to the powerfull use of
this RegEx class, so I wonder if it could go a step further.

As a question, can regex be used to valid a set of different
functions ? Example : Suppose I have to verify the correctness
of an input string, which may contains one or more of the
following functions :

Round ( NumericValue, Decimals)
Lower( StringValue )
Upper( StringValue )
Abs(NumericValu e)

... it will be like 15 functions, but let's name just this
three.

Note : I just want to validate the input, I don't pretend to
perform the resolving part of this functions, just validate the
input in terms of : 1.- Data type of parameters.
2.- Pairing parenthesis.
(the resolution of the of the functions will be done by 3rd
party's code).
So, if I receive :
Abs("VB is great").

I would reject that sentense due the characters between
parenthesis are a string, not numeric values.

But, instead if I receive :
Upper( "C# is the best thing since sliced bread")

I would accept the sentence because the parameter is of the
proper type.

Also:
Round( 1234.56, 2

would be invalid, due the missing parenthesis.

Finally, the functions can be nested.
So, the question is : can Regex handle this ? or should I start
to go for the parsers libraries ?


Tim,

Taken individually, each function's form could be validated by a
regular expression. For 15 functions, you would need to write 15
regexes.

Taken together, however, the complexity of matching arbitrarily
nested function calls will quickly turn any regex-based solution into
an unmaintainable mess. This is assuming it's even possible to do
with regexes. Assuming the following would be valid input in your
system, I have no idea of how to write a generic regex to validate
this:

Upper(Lower(Upp er(Lower("())() (((()()())"))))

I would suggest investigating lexers and parsers. They're not that
hard to write, and can handle the above input with ease (and much
more complex input as well). For a gentle introduction to writing a
parser from scratch, here's a good site:

"Let's Build a Compiler" by Jack Crenshaw:
http://compilers.iecc.com/crenshaw/

It's written in Pascal, but it shouldn't be too hard to port to C#.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4650
by: Brian Richmond | last post by:
I'm trying to use a regular expression to match a hidden html tag and replace it with the results of a mysql query. The query is based off a part of the hidden tag. For example: The article looks like this ("Math" is the value I want to pass to my function call, it could be any value for the department name): $article = "Below is a...
3
1878
by: Guy Robinson | last post by:
I have the code below which parses an expression string and creates tokens. Can anyone suggest the best of error checking for things like: Valid variable only obj.attribute -whitespace allowed test( "ff*2/dd.r..ss r") #additional ..ss -invalid variable. test( "ff*$24..55/ddr") #double .. and $ -invalid number test( "ff*2/dd.r.ss...
7
5715
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning is to improve, but not to prove.
9
4565
by: Tim Conner | last post by:
Is there a way to write a faster function ? public static bool IsNumber( char Value ) { if (Regex.IsMatch( Value.ToString(), @"^+$" )) { return true; } else return false; }
6
4782
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search string. It's actually the input string into a Microsoft Index Server search. The string will consist of words, perhaps enclosed in quotes or...
7
2835
by: melanieab | last post by:
Hi, I'm trying to use DataView to find the row number in the datatable that contains "Rich" in it so that I can highlight it. It works fine when I enter the entire string (i.e. Richard), but I can't seem to make a search for "Rich" recognize that Richard is also what I want. The problem seems to be here: DataView dv = tCat.DefaultView;...
6
5867
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead". This parrot is sleeping. Really, it is sleeping. to This parrot is dead. Really, it is dead.
15
50174
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
16
2237
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string) pattern search functions. Where performance is an issue you can alway write your own specialized routine of course. However, for the occasional...
0
7666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.