Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everyone,

I am new to regular expressions. I need a regular expression to read the values between two parenthesis. Basically the regular expression should fetch the operands from a logical expressions.

For example:

INPUT :

1. ((infx.Length != i) && (ch = infx[i++]) != '\0')
2. (infx.Length != i && infx[i++]) != '\0')


OUTPUT :

1. infx.Length != i
ch = infx[i++]) != '\0'

2. infx.Length != i
infx[i++]) != '\0'

Thanks in Advance
Posted
Comments
s#@!k 19-Feb-14 2:00am    
your question is not clear, you can improve your question and provide some code what you have tried.

To be honest, a regex is a poor way to do this: regular expressions are very good at pattern matching, but they are very poor are syntactic analysis, which is what this task requires.

It is possible to match brackets and so forth - though it is quite complex - but it can't easily spot things like this:
(ch = infx[i++]) != ')')

I would look at tokenising the expression, and using that for your evaluation.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Feb-14 2:57am    
Great example to fail the naive approach, my 5.
(Your brackets are unbalanced, but the idea is clear.)
—SA
There should be three parts in the output, the third one should be
!= '\0'

Anyway, regular expression is not for everything, try the following approach:
C#
string originalString = @"((infx.Length != i) && (ch = infx[i++]) != '\0')";
Console.WriteLine("The original string is {0}", originalString);
// more separators can be added here
char[] charSeparators = new char[] {'(', '&', ')'};
string[] result = originalString.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in result)
{
    if (s != " ") //ignore white space
    {
        Console.Write("{0} \n",  s);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900