|
Hey Everyone,
I'm looking for some ideas on how to find the indexes of nested parentheses () in a string.
Basically the user is going to type a complex mathematical formula and i would like to know where all the nested ( and )'s are.
I have considered RegEx, but i had some real trouble with the nested part and i dont want to remove that functionality)
Any Ideas?
If at first you don't succeed ... post it on The Code Project and Pray.
|
|
|
|
|
Have a look at Expresso[^] - it is a regex creation / testing tool. One of its pre-built expressions is:
\(
(?>
[^()]+
| \( (?<number>)
| \) (?<-number>)
)*
(?(number)(?!))
\)
Which is a parentheses matcher. May be a good start for you?
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Thanks for the reply, this a start for sure. I could use something like this then just recursively go through the matches finding all the nested pairs.
Thanks for the reply
If at first you don't succeed ... post it on The Code Project and Pray.
|
|
|
|
|
Hi,
you probably want to create a parser for your expressions, not just some code about parentheses.
So you first of all need a parser that understand identifiers, literals and operators, with operator precedence (identified by a value in a limited range, say add=0, sub=0, mul=1, div=1, etc up to 9). You need a little stack to hold operands, push operands as long as operator precedence is increasing, and popping operands and executing operators when precedence is decreasing or constant (and something special for right-to-left associativity as in a^b^c).
Now there are two ways to extend to expressions with parentheses:
1. the easy and slightly more expensive one uses recursion:
- create a method that implements the simple parser, and start scanning the input;
- once you hit a left parenthesis, you know a subexpression is going to start, hence let your method
call itself to parse that subexpression
- once you hit a right parenthesis or end-of-input, return.
2. the other way basically implements one overall parser:
- scan the expression left-to-right doing all the things your parser needs to do;
- once you hit a left parenthesis, adapt the current state and increase the precedence value of all future operators causing them to get executed sooner than everything outside the parentheses.
While (1) seems simpler than (2) when everything is fine, the recursive one becomes a little more complex if you want it to deal with mistakes properly: you have to unwind the recursions when invalid expressions are being fed, and you need to figure a way to generate proper error reporting, all of which is much simpler in a single overall parser.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hey Luc,
As per usual your answer is extremely well thought out and just all around a very good answer.
However; I already have the validation and the execution of the formula figured out.
The problem is that I am trying to add some visual indication of the operator precedence using a RTF textbox and some extremely simple syntax highlighting.
I can find all the operators (even the ()) but the problem is that i would like to use a different colour for each pair of () in the formula.
Thanks again for your input.
If at first you don't succeed ... post it on The Code Project and Pray.
|
|
|
|
|
Hi Adam,
you're welcome.
if all you need is locating parentheses and assigning some colors, I wouldn't use any Regex, because (1) I'm not really familiar with them and (2) I find them both powerful and cryptic, yielding hard-to-read code.
So I would just search for the next string.IndexOfAny('(',')') and then check:
for a '(' increment your color index and apply
for a ')' apply color, then decrement color index,
where color index is indexing an array of colors in round-robin fashion.
Personally I don't like multiple colors for the same token, so what I usually do is choose only one color for all parentheses (or brackets or whatever comes in pairs), then use a second color for such token and the matching one when the cursor is right behind one.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
|
unfortunately not, nice article though.
Thanks for the reply.
If at first you don't succeed ... post it on The Code Project and Pray.
|
|
|
|
|
hi to all
i would like to ask, is it possible that if i create a system and i want to generate the display items into excel file or txt file is it possible??
Warm Regards
Veon
|
|
|
|
|
Yes.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
LOL, i was going to answer the exact same way but i decided against it.
LOL though
If at first you don't succeed ... post it on The Code Project and Pray.
|
|
|
|
|
|
Adam R Harris wrote: Well anything IS possible
No CG got to say no to the open folder detection question
Just that this question is not totally invalid.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
Have 5 - some of the questions here seem to be just plain silly, and then someone comes up with an answer.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi all
how can i find specific values in the List<> class?
there is "
public T Find (Predicate <T> match)" , FindAll and findindex class functions ... but there all using the
Predicate Delegate function , the search value is fixed in the Delegated function (as seen in the example in MSDN).
is there way to search the list<> in Generic function specific values without using for each loop or any other loops?
Thanks
|
|
|
|
|
Think about this question for a moment.
Is there a way to search through the entire collection, without looking at each item ? The answer is yes, but only if you sort your collection. Then you can make each search step divide the existing collection in two. Otherwise, it's obviously not possible, that kind of goes without saying.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
pascal.schwartz wrote: the search value is fixed in the Delegated function
What do you mean by that?
|
|
|
|
|
From the MSDN the search value "saurus" is fixed, the EndsWithSaurus is the Predicate Delegated function
used
dinosaurs.Find(EndsWithSaurus));
dinosaurs is List of string object
private static bool EndsWithSaurus(String s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}
|
|
|
|
|
Hi,
I understand your pain. Wouldn't it be nice to be able to write:
dinosaurs.Find(EndsWith("saurus"));
A little bit of code is required to get a variable into the predicate.
public class StringPredicate {
String stringToMatch;
public StringPredicate(String toMatch) {
this.stringToMatch = toMatch;
}
public Boolean EndsWith(String s) {
if ((s.Length > 5) && (s.Substring(s.Length - 6).ToLower() == this.stringToMatch)) {
return true;
} else {
return false;
}
}
}
Now you may call the List.Find method like this.
dinosaurs.Find(new StringPredicate("saurus").EndsWith);
Easy when you know how!
Alan.
[edit] Version2, a slight rejig of the code
public class StringPredicate2 {
String stringToMatch;
private StringPredicate2(String toMatch) {
this.stringToMatch = toMatch;
}
private Boolean Match(String s) {
if ((s.Length > 5) && (s.Substring(s.Length - 6).ToLower() == this.stringToMatch)) {
return true;
} else {
return false;
}
}
public static Predicate<String> EndsWith(String searchTerm) {
return new StringPredicate2(searchTerm).Match;
}
}
Now you may call using a more natural form:
dinosaurs.Find(StringPredicate2.EndsWith("saurus"));
modified on Monday, August 3, 2009 9:34 AM
|
|
|
|
|
So you want a generic find method which can find any string in your list, right? Try this,
string Find(List<string> list, string itemToFind)
{
return list.Find(item => item == itemToFind);
} Lambada expressions makes it very easy.
|
|
|
|
|
If it's a list, you need to search it. (And there could be duplicates.)
If you convert it to a HashSet you don't.
|
|
|
|
|
how can i program a microcontroller in c#??
i want asource code for the main programming
|
|
|
|
|
lollipop1312 wrote: how can i program a microcontroller in c#??
i want asource code for the main programming
Well, I doubt you can, but I'd try to write you one for a standard contract rate ( say, $120 an hour ). You didn't expect someone to write it for free, did you ?
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
thanx
but dont u think thats too expensive
|
|
|
|