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}