Tags

,

The Problem
I like to copy the code to Eclipse or NetBeans to read it and run it when read some interesting code in internet. The code in the web is usually not well formatted: many empty lines. This makes it harder to read the code.
So I like to remove empty lines  to make the code looks smaller and concise.

Here regex comes into play.

Remove Empty Lines

//<![CDATA[
if(showAdsense){
document.write("(adsbygoogle = window.adsbygoogle || []).push({});”)
}
//]]>

Find: ^\s*\n
Replace: (empty)

Merging Empty Lines
Find: ^\s*\n
Replace: \r\n or \n in windows 

Next, use Ctrl+Shif+F in Eclipse or Alt+Shif+F in Netbeans to fromat the code.

Online Regex Tools
http://regex101.com/
Here it will explain the meaning of the regular expression, we can test it.

  • /^\s*\n/
    • ^ assert position at start of the string
    • \s* match any white space character [\r\n\t\f ]
      • Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
    • \n matches a fine-feed (newline) character (ASCII 10)

http://ift.tt/15sGP4l
Here we can test our regualr expression in almost all languages, it can also give its java or .Net String, so we don’t have to manually escape special chracters such as \ or ” in the regex string.

via Blogger http://ift.tt/1fe0sPc

Advertisements