473,700 Members | 2,616 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 3422
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
4659
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 listing of the staff in our Math department:<p><!-- Directory:Math -->"; I'm using this preg_replace...
3
1890
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 r") #variable with double . -invalid variable
7
5726
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
4583
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
4793
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 parentheses. I'd like to use Regex to pull out the words, or the phrases if the words are enclosed in quotes....
7
2840
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; Regex reg = new Regex(@"^Rich");
6
5892
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
50249
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
2247
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 pattern search where performance isn't an issue, would most seasoned .NET developers rely on "Regex" and...
0
8731
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8648
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9218
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8975
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8929
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4408
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2031
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.