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, 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,
"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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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.
|
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;
}
|
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...
| |
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;...
|
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.
|
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
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |