• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help Improve Regex Parsing

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A while back I posted a problem with recursive syntax and regex. I couldn't do what I really wanted so I'm doing what feels like a bit of a hack. I got regex to find the innermost macro call in a nested expression. In a loop I replace the innermost with the results of the macro call and search again. Next time I find the new innermost call.

Here's an example of nested macros. The page macro returns name, size, date etc info about a page. The linksto macro returns a list of reverse links.

${linksto ${page name}}

This works, but I'm keen to know if somebody sees a better way. I can post more code if you really want to exercise this stuff but there's a bit of a framework growing under it.
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If doing the replacements becomes a bottleneck, you could write a fairly simple and efficient parser to do the same thing, processing the String into a StringBuffer top-down.

With the iterative regex solution, each replacment builds a complete modified copy of the input String: 10 replacements in a 1k String requires roughly 10k in new -- albeit teporary -- Strings. If, however, you will only be doing a few replacements in small Strings, it may not be worth the bother.

It would be a good exercise though.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan, that seems to be a good simple solution to me. If it doesn't show up as a performance problem, I'd simply keep it that way. (If someone has a better idea, I'd be interested too, of course!)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic