I had this code fragment, formatted with HTML and I needed to figure out the amount of "real" lines it actually contained.
code-html sample: (it has <pre> tags around it, how pretty :P)
oos.flush();
} <span style="color: rgb(0,0,128); font-weight: bold;">
catch</span> (IOException e) {
System.err.println("Oops, error, don't worry stream will be closed...");
} <span style="color: rgb(0,0,128); font-weight: bold;">
finally</span> {
<span style="color: rgb(0,0,128); font-weight: bold;">
try</span> {
<span
style="color: rgb(0,0,128); font-weight: bold;">
if</span> (oos != null) {
oos.close();
Because this big String contains lines I don't need to count, like these 2<span style="color: rgb(0,0,128); font-weight: bold;">As well as the doctype on top, the head tags, the title, the body tags, html tags, etc etc.
I had the WONDERFUL idea to go write a regex which will only match the ends of lines I need.
After an hour or so I came up with
^((([^<>\n]*<.*>[^<>\n]+)*)|([^<>\n]*))$(And that works ^^)
*goes on a break*
...
*comes back*
*thinking: Dude, this is Java. Every line ends with either a semicolon or curly braces if it's written properly*
result:
^.*[;{}]$
And added "|^$" to match empty lines too.So I just rewrote the 1 hour regex in 5 minutes. :glare:
Really one of those moments where I wanna do this:

(Okay, you can write Java on multiple lines without using a semicolon if it's the same statement, but that was not happening in my htmls)
So guys and girls. Breaks are wonderful :)


Sign In
Create Account


Back to top









