Tuesday, October 13, 2009

My regex Notes

These are notes I have created for regex. It is not formatted right now but I will update this post soon.

[] = use square brackets [] as OR operator. You can also call it a character class.
[^xyz] = ^ in the beginning of [] means "(not x) or y or z"
^abc means start char must be a
abc$ means end characted must be c
example:
^\s+ = beginning with whitespace-charater one or more times
\s+$ matches trailing white space
in perl trim function can be $input =~ s/^\s+|\s+$//g

Word boundaries:
[a-zA-A0-9_] are word characters. [a-zA-A0-9] = \w
\b is used to define boundary
\bis => "my name is priyank" and "I like Hawai islands" will match. But "this was a book" won't.
\bis\b => Only "my name is priyank" will match.
is\b => "my name is priyank" and "this was a book" will match "I like Hawai islands" won't.

Regex is Eager
ref: http://www.regular-expressions.info/alternation.html

You use "?" for optional items. ? means 1 or 0 times
B4?U matched both BU and B4U.
(Jan)?uary mathces Jan and January
* means 0 or more times.
+ means 1 or more times.

You use "?" to make regex non-greedy.
So in "tennis is a nice sport. ", if your regex is (.*is)will match the string "tennis is" but if your regex is "(.*?is).*" will find only "this".
SO "?" makes the regex lazy or ungreedy.

Round brackets are used for grouping.
Round bracket also create a 'backreference'.
What is a "back reference" in regex?
Ans: A backreference stores the part of the string matched by the of the regex inside the parantheses.
Backreference slows down the regex engine as it has to some more work.
You use "?:" to tell regex engine not to create any back reference.
How to use a backreference ?
Backreference allows you to re-use matched part of a regex. You can re-use it inside the regex itself.
A very good example ofr backreference is find starting and closing html tag ans contetnt inside that tag.
<([A-Z][A-Z0-9]*)\b[^>]*>.*?


Download regex cheatsheet

Sunday, October 11, 2009

How to show line number in VI editor

Use following command to view line numbers
:set number

To remove line numbers, use following command.
:set nonumber

Wednesday, August 26, 2009

Twitter bookmarklet

Updated version of this bookmarklet is here Twitter bookmarklet


What does this bookmarklet do?

You are reading a great article on the web or you found a great website. Now you want to share it with everyone on twitter. Just click on "TweetThis" bookmark in your bookmarks tab and it will take you to twitter with url already shortened with bit.ly.

Drag this Twitter Bookmarklet to your favorite browser's Bookmarks Toolbar.


Old: TweetThis Check out the latest one below.

UPDATED bookmarklet - includes page title in a your tweet.

New: TweetThis

1. You found a great article on yahoo and you want to share it on twitter. To do this- Click on TweetThis bookmarklet.



2. Once you click on the bookmarklet, it will create bitly url for the webpage and take you to twitter.com to post.


If you want to view what it is doing, check this.

Source code:
javascript:function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);}var url = 'http://api.bit.ly/shorten?version=2.0.1&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&longUrl='+document.location;var longUrl=document.location;loadScript(url+'&callback=tweetme');function tweetme(json){var shortLink = json.results[longUrl].shortUrl;document.location='http://twitter.com/home?status=I+found+this+great+thing+'+shortLink}


Friday, August 14, 2009

Syntax highlighting in vi/vim editor on ubuntu

To turn on syntax highlighting, use this command. :syntax on
When you open any file in vi editor, press escape and enter :syntax on.
Ubuntu by deafult has vim-tiny installed. So syntax command may not work. You may get this message: "E319: Sorry, the command is not available in this version"

ubuntu vi editor screenshot

To install full version of vim, use following command.

$ sudo apt-get install vim
or
$ sudo apt-get install vim-full
You can also check which package is ubuntu using by entering following command.
$ dpkg -l vim*
priyank@priyank-laptop:~$ dpkg -l vim*
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Cfg-files/Unpacked/Failed-cfg/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)
||/ Name Version Description
+++-=======================-=======================-============================================
ii vim 2:7.2.079-1ubuntu5 Vi IMproved - enhanced vi editor
ii vim-common 2:7.2.079-1ubuntu5 Vi IMproved - Common files
un vim-doc (no description available)
un vim-gnome (no description available)
un vim-gtk (no description available)
un vim-lesstif (no description available)
un vim-nox (no description available)
ii vim-runtime 2:7.2.079-1ubuntu5 Vi IMproved - Runtime files
un vim-scripts (no description available)
ii vim-tiny 2:7.2.079-1ubuntu5 Vi IMproved - enhanced vi editor - compact version

Once you install vim, and open any file using vi/vim editor turn on syntax highlighting by using the commnad given above. You should be able to see your code like this.

Thursday, August 13, 2009

Voyij.com travel deals widget

Latest Travel Deals powered by Voyij

Tuesday, August 4, 2009

UNIX: useful applications list (opensource)

I came across this website - thanks to Bing.
It contains list of applications every UNIX/Ubuntu user should have.
The list includes following categories.
- Audio Applications
- Graphic Design Applications (2D)
- Graphic Design Applications (3D)
- Video Applications
- System Applications:
- Developer Applications
- Productivity Applications
- Web Development Applications

Some the applications that I am using or I have installed are:
GNOME Do
Basket Note Pads
Pidgin
jEdit
Audocity
GIMPshop
Wine
OpenSSH
OpenOffice
Putty
bugzilla
Eclipse
Geany
TrueCrypt

Wednesday, July 29, 2009

Installing and configuring mysql on ubuntu

Open terminal.

Enter following commands.

$ sudo apt-get install mysql-server mysql-client
$ mysql_secure_installation

First command will install mysql server and client both.
Second command is not required butif you want to make your database more secure, it is helpful.
Default port for mysql is 3306. If you wish to change this, you can do so by editing mysql configuration (my.cnf) file which is located under /etc/mysql.

$ sudo vi /etc/mysql/my.cnf

Look for string port=3306
Update it to your desired port number.
Once you save my.cnf file, you will have to restart mysql server to effect your changes.
You can use following command (remove curly brackets) to start,stop or restart mysql server.

$ service mysql {start|stop|restart}


Wednesday, June 3, 2009

JAVA: PatternSyntaxException: Dangling meta character '*' near index

Pattern Syntax Exception is thrown when you have "*" in any String and you want to replace "*" with something else.
This exception is likely when you use method java.lang.String.replaceAll().

There are two solutions to this problem.
Solution 1: escape all metacharacters and use replaceAll().

Solution 2:
I prefer this solution over solution 1. This method reduces most of the work as you don't need to escape any metacharacters.
Just use org.apache.commons.lang.StringUtils

Wednesday, May 20, 2009

Information about Internet Explorer and CSS issues

I found out this website when I was fixing IE and Iframe issues for a website.
This website contains lot of good and important information about different CSS issues you will face in Internet Explorer.

The article talks about following issues.
  • Narrower page elements in IE
  • Text spilling out of container in Non-IE browsers
  • Disappearing background images
  • Widths only working on IE
  • Unstyled version of web page appearing in IE
  • Fixed width webpage center alignment issue
Click Here

Tuesday, May 5, 2009

Pidgin + Twitter integration

Pigdgin + TwitterInterested in twittering from your IM???
You can also "chat" with your twitter contacts.
There is an interesting plugin microblog-purple for pidgin which enables twitter in conversion windows.
This works on both Windows and Unix.
Follow link: microblog-purple

For Windows, download this file.

Thursday, April 30, 2009

UNIX: File permissions

How to modify file permissions

There are three types of permissions: Read, Write and Execute
And there are three groups you have to consider while changin permissions for a file. user, group (which user belongs to), others (Other users outside of the group)

Use command:
$ chmod 777 file.txt
777 = 7-> user, 7-> group, 7->others
Binary value of 7 is 111 = Read->1, Write->1, Execute->1
So 111 for all three groups.

755 = 7-> user, 5-> group, 5->others
Binary value 7 is 111.
Binary value of 5 is 101 = Read->1, Write->0, Execute->1
So only you have the permission to read, write and execute a file.
Other users can read or execute the file but cannot write.

Still editing this post. Will update soon.

Monday, April 13, 2009

JAVA: IOException - LocalRMIServerSocketFactory

"The server sockets created using the LocalRMIServerSocketFactory only accept connections from clients running on the host where the RMI remote objects have been exported"

If you r using ubuntu and while running jconsole you are getting this message, then you should comment 127.0.1.1 ~domain from /etc/hosts file.

$ sudo vi /etc/hosts
127.0.0.1 localhost
#127.0.1.1 ubuntu


This is Ubuntu specific bug.

For more information visit:
This link

Thursday, April 9, 2009

JAVA: illegal group reference

How:
You have a string "I like dollar symbol".
and you want to replace the string with "I like $ symbol".
Code:

String a = "I like dollar symbol"
String replacement = "$"
a = a.replaceAll("dollar", replacement);


This will throw an exception "Illegal group reference."

java.lang.IllegalArgumentException: Illegal group reference
at java.util.regex.Matcher.appendReplacement(Matcher.java:713)
at java.util.regex.Matcher.replaceAll(Matcher.java:813)
at java.lang.String.replaceAll(String.java:2190)


Solution:
Here the problem is "$" is a Metacharacter which should be escaped.

  • replacement = Matcher.quoteReplacement(replacement);
    a = a.replaceAll("dollar", replacement);


    About quoteReplacement method in Matcher class
    Ref. link: Click here
    Returns a literal replacement String for the specified String. This method produces a String that will work use as a literal replacement s in the appendReplacement method of the Matcher class. The String produced will match the sequence of characters in s treated as a literal sequence. Slashes ('\') and dollar signs ('$') will be given no special meaning.

  • put two backslashes before "$".

    String replacement = "\\$"



Tuesday, March 31, 2009

UNIX: 'export' vs 'setenv', what is the difference

Both of them have same purpose but which one to use depends on what shell you are using.

If you are using sh and ksh shells, you use 'export'

If you are using csh, you use 'setenv'