• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regular expression for a formula

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,i have an expression like this :SUM(10,20,ABS(-5)) it is a formula like a formula used in Excel .it is possible to match this expression using regular expression ??? the purpose is to validate an expression and generate an object from this expression.thanks
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Med. Welcome to the Ranch!

It's probably possible. But it isn't enough to just give an example. I can give a regular expression that trivially matches that particular example and nothing else. You need to be able to describe the rules that apply. What are the allowed expressions, and how can we identify something that *doesn't* match?
 
med yousfi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the regular expression must be generic and accept the fact that a parameter of a function can be a function for exemple Sum(a1,Average(a2,a3,abs(a4))) a complex expression in this case Average(a2,a3,abs(a4)) is a parameter of Sum and abs(a4) is a parameter of the the function Average...
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again you've just given an example. You need to define the rules. Personally I would look at using a lexical parser rather than regular expressions.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regular expressions are not powerful enough to match mathematical expressions of arbitrary depth. Look into parser generators like SableCC, JavaCC and Antlr; they probably have predefined grammars for mathematical expressions. Or use a lexer like JFlex; I've yet to come across something so complicated that I felt like I *needed* a parser generator; JFlex was always sufficient for my purposes. Your mileage, of course, may vary.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

med yousfi wrote:the regular expression must be generic and accept the fact that a parameter of a function can be a function for exemple Sum(a1,Average(a2,a3,abs(a4))) a complex expression in this case Average(a2,a3,abs(a4)) is a parameter of Sum and abs(a4) is a parameter of the the function Average...



All of the examples you have given can be matched with

.*

That matches zero or more characters. But it probably isn't what you want.

I think you are putting the cart before the horse. You have decided to use regexes, and are asking how to force them to do what you want. That's like saying "I have a hammer - how do I use it to drill a 1/4" hole in a wall?"

sure, you MIGHT be able to do it if you really work at it, but it is not the right tool.

Instead, you should look at what you are trying to do, and then figure out what the right tool to do that is.
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic