This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Anyone know regular expressions?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,
i'm using regular expressions to filter thing from a String.
Can a regular expression be recursive?
I need a expression (that works in org.apache.regexp.RE) that parses something like a function. A function starts with '${' and ends with '}'. But in the text between there can also be new 'instances' of ${...}
Eg.
When parsing the string "bla bla${bla ${bla} ${bla}}bla ${bla} bla" the first thing the epxression needs to give me is "${bla ${bla} ${bla}}"
I'm not very good with expression and have been using \$\{.+?\} as an expression at the moment, but that wont give me the right results.
Does anybody have a good idea for a working expression? Or should i be writing my own parsing routine?
Thanks in advance,
Robert Willems
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
moving this to the Java In General (Intermediate) forum for two reasons:
1- this isn't a very advanced question.
2- Max Habibi is a moderator there and he wrote a whole book on Regular Expressions!
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the "${...}" constructs can be nested more than one level deep, you'll have to write your own parser. It's the classic "nested delimiters" problem: regexes can't handle them. However, if you know they'll never go more than one level deep, you can fake it with something like this:

This assumes that, in well-formed text, the '$' character will never appear without the "{...}", and vice versa; if that isn't the case, a more complex regex will be needed.
 
Robert Willems
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alan,
too bad regexp's cannot be recursive... Oh well we'll just call it an undocumented restriction that those constructs cannot be nested ;-)))
Thanks for your reply...
Regards,
Robert Willems
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Robert,
I took a crack @ this, and couldn't come up with anything purely regex: sorry if that busts any illusions you had about authors .
You might want to consider a recursive method call. The method could use regex internally.
All best,
M
 
Robert Willems
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Max,
thanks for your reply... my illusions are totally shattered
... but is it possible to 'count' occurences of a string in a regexp? So i can do something like:
- The selected string has to start with ${
- It has to end with }
Not so difficult until this point ;-), but can you do something like:
- It has to contains as much occurences of { as } ?
Because that was another of the main problems.. if i counted on the wellformedness of the string it would be either to reluctent on grabbing characters (returning ${bla {$bla}) or to greedy (returning ${bla ${bla} ${bla}}bla ${bla})
Regards,
Robert
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the format is always ${..}, not sometimes {..}, this might work (needs testing)
 
Evildoers! Eat my justice! And this tiny ad's justice too!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!