• 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

Split each expression in String

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to split each expression in the below String (string can have any combination of criteria)

e.g., name eq 'alex' or role eq 'admin' and salary gt '3000'

Need to split it as below
name eq 'alex'
role eq 'admin'
salary gt '3000'

(I have a RESTful api that might have different combination of unexpected criteria in the uri)

Do i need to write any regular expression for this?
 
Marshal
Posts: 79508
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try splitting on "and|or|implies|iff|xor" or similar
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
an example is great, but it doesn't make a spec.

Can you split on spaces, then recombine three and discard the fourth?
 
Vani Kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That works, but i can have different number of combinations in the string like below

example 1:
"role eq 'admin' and department eq 'science'"

example 2:
"name eq 'vick' and role eq 'admin' and salary gt '3000'" or status eq 'active'

example 3:
"(address/zipcode eq '11122' or address/zipcode eq '22233') and (name/lastname eq 'xang' or name/firstname eq 'john') and status eq 'active' and contains(familyName, 'smith')";
 
Vani Kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, to be more specific, i need to get like below e.g., for example 3

(address/zipcode eq '11122' or address/zipcode eq '22233')
and
(name/lastname eq 'xang' or name/firstname eq 'john')
and
status eq 'active'
and
contains(familyName, 'smith')

and then again i can split it like below

address/zipcode eq '11122'
or
address/zipcode eq '22233'
and
name/lastname eq 'xang'
or
name/firstname eq 'john'
and
status eq 'active'
and
contains(familyName, 'smith')
 
Campbell Ritchie
Marshal
Posts: 79508
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the associativities and precedences of the different operators, and, or, etc.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Based on the examples, there are a few concerns -- concerns that make the parsing complicated.

Vani Kumar wrote:
example 1:
"role eq 'admin' and department eq 'science'"



First, the identifier may have "and" or "or" embedded in it. For example, what about "neighborhood eq 'kew gardens'". There is actually an "or" in neighborhood.

Second, the quotes could contain anything. For example "conjunctions eq 'and or xor not'". And because of that, you will need to track the quotes, so you don't split when you are within the quotes.

Vani Kumar wrote:
example 3:
"(address/zipcode eq '11122' or address/zipcode eq '22233') and (name/lastname eq 'xang' or name/firstname eq 'john') and status eq 'active' and contains(familyName, 'smith')";



And third, and this is a big one... you have to deal with precedence. In this example, the "or" within the parenthesis has higher precedence that the "and" outside of the parenthesis. You can't just split it directly. It has to be split into the parens components, and then when processing the parens components, it has to be split again (recursively).

Henry
 
Vani Kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like split from string or commons stringutils (i tried to check if substringsBetween(string,"(",")") , subssubstringBefore work, but will miss the precedence operators like and, or) might not work for this case. Any other ideas, please suggest.

Right now i might not care about precedences of operators (i think may be the criteria in jpa can handle when i add all the expressions to it, not sure still, i'll double check)
 
Campbell Ritchie
Marshal
Posts: 79508
379
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect that precedences and associativities would take your expressions out of the realms of regular grammars and into the realms of context‑free grammars, so you would need a proper parser as regular expressions can only parse regular grammars.
 
Vani Kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't have quite experience on parser and regular expressions as well. Please suggest any ideas. Thanks!

Just found some parser generators online, will do some research, learn and will check my luck.
 
Campbell Ritchie
Marshal
Posts: 79508
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to start by defining a grammar for your language.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You need to start by defining a grammar for your language.


I would say you need to define the problem. Every time someone makes a suggestion, you come back with "actually, I need to do THIS" or "actually, I don't care about THAT". So until you can clearly define what you do and don't want to do, there's not much more advice anyone can give you.
 
Vani Kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I realized this feature can be achieved by using libraries like java framework/library like Odata4j, Apache olingo.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic