Originally posted by Ulf Dittmer:
Most regexp libraries can perform multiline matching. Search the javadocs of the java.util.regex.Pattern class for "MULTILINE".
Originally posted by Piet Verdriet:
Try something like this:
But it will still break in a lot of cases! Better use a true Java source parser. ANTLR is an impressively easy to use parser generator.
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Originally posted by Andr� Campanini:
Hello!
I got what I wanted doing this:
regex = "(?m)void myMethod\\(\\)\r\n\\{\r\n\\}"
String newFileContent = fileContent.replaceAll(regex, "/*"+regex+"*/");
It replaces every method with coments... just what I wanted. I don't know if it is just right using regex this way... but is working, now...
Regards!!!
Originally posted by Andr� Campanini:
The method HAS a body, I just didn't put it here this way in the example.
Originally posted by Charles Lyons:
...Is that really difficult? Surely easier than regex?
[/QB]
I don't agree with that at all, based on my own experience writing mathematical parsers which did a similar thing. There are only a few places where an opening or closing brace can legally occur and where it doesn't group code into a block (which is part of the counting mechanism). If you don't believe me, read the Java Language Specification. Yes, there are plenty of examples of invalid code which will mess up the conversion, but they will also fail to compile so we can ignore them.That's just the point: you can't simply catch all "special cases" in just one method: you need a true recursive decent parser.
Originally posted by Charles Lyons:
...
You saying one can't do it this way
...
Originally posted by Piet Verdriet:
...
My point here is (towards the OP): there exists no simple method
...
My apologies if that was the impression I conveyed. By "easy" I mean that it doesn't involve any complicated code or advanced libraries, and that it is a short program (I would hazard that the bulk of it can be accomplished in under 50 lines). The only techniques which need to be used are elementary string processing operations (namely charAt and indexOf). Naturally, as with all programs, you have to carefully think through the problem to come out with a decent and high-performance solution; isn't that part of the satisfaction that comes from writing a good program? Whether the solution is trivial depends on what background one comes from and how much experience they have (this is the intermediate, not beginner's, forum after all). Regardless, this can be done in a straightforward piece-by-piece way by thinking only about processing characters in a string linearly, starting with the simple program I gave and then building supplementary rules to cover the few special cases that exist. All good software is built in stages or modules, and this is no exception. I deliberately left the OP to think carefully about what would go in the // TODO bits of my code, since as you emphasised, those are the bits which require some intelligence!it isn't as trivial as I had the impression you lead the OP to believe.
Originally posted by Charles Lyons:
My apologies if that was the impression I conveyed. By "easy" I mean that it doesn't involve any complicated code or advanced libraries, and that it is a short program ...
You got style baby! More than this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
|