Thursday, July 19, 2007

Regex with Named Groups

Suppose you want to remove a middle name from name you can use “replace” method in your favorite language/editor in this way..
You Regulare Expression: (?(\w+))\s*(?(\w+))\s*(?(\w+))\s*

Replace with: ${ FirstName } ${ LastName }

Some days before when I needed help about this, googling didn’t help much. If you come across any good link about this, don’t forget to put a comment here.

(This may be a bad example but I am using it just for simplicity and the sole objective of this blog is to understand 'named group' or 'variable')

[Updates]
Tutorial Link:
http://www.regular-expressions.info/named.html

Thursday, July 05, 2007

AndAnd and only And

From my little experience itself, I’ve seen various ‘demonic’ forms of condition that we put into ‘if’ (or ‘while’ loop). Sometime the combinations of ‘And’, ‘Or’ and ‘Not’s will scare hell out of you. But here is a classic case: Suppose you are applying ‘And’ condition and (in c# at least) you have used ‘&’ (which is bitwise operator) instead of ‘&&’. In most of the case this won’t give any problem unless you do something like this,

If( a != null & a.Count > 0)
{ do_something; }

It will catch you and exception will be thrown saying you want something which doesn’t exist.

NOTE: ‘&&’ is totally safe for above example as && will evaluate the second condition only when the first is true.

Friday, April 20, 2007

‘AND’, ‘OR’ and ()

Sometime we don’t care about brackets at right position, especially when giving conditional statement. I guess everyone has put himself in trouble at leas once just for this.
A simple example in SQL query (make sure that your table i.e. myTable contains some records),

select * from myTable where 1=1 and 0=0 or 1=1 and 1=2
select * from myTable where 1=1 and (0=0 or 1=1) and 1=2

And it becomes worse when output chagnes depending upon platform in case of statement without any brackets.

Tuesday, March 20, 2007

RegExp... Express your strings !

‘Regular Expression’ is powerful concept of describing the format of string and validating (i.e. matching) the string based on expression. This is supported by most of programming/scripting languages, and notably well suppported by Perl, Python, all languages on .NET platform, Java and Javascript. This concept has gone beyond the programming language and new text editors or IDE have started giving support for regular expression for Search/Find text and Replace text, but regular expression is best used for validating user input on client side (using javascript) in web applications.

Lot of people know about Regular Expression but do not use them when required. This is because it's more complex and time comsuming to write at begging. But when you mast the underlying comcepts, it's all done.

Here are some good links that you can use in day to day life of a programmer.

  1. If you are using firefox browser this extension is for you: https://addons.mozilla.org/firefox/2077/
  2. Regular expression Tester: http://www.regextester.com/
    • This is online tester for regular expression. It accepts more than one input and validated results immediately in result window as you change you expression. This is my favorite for writing regular expression.
  3. http://regexlib.com/default.aspx : This is library for regular expressions. Most probably you will find the one you want.
  4. http://www.regular-expressions.info : Excellent in details tutorial for regular expressions.
  5. http://www.evolt.org/article/Regular_Expressions_in_JavaScript/17/36435/ : Quick reference. A good article to refer after having basic knowledge about regular expression.
  6. Have you thought about having text editor with support for regular expression for finding and replacing the string? SciTE is the powerful, lightweight text editor which has support for regular expression along with lots of other useful features. Go to http://scintilla.sourceforge.net/SciTERegEx.html to see how work with regular expression in SciTE.
Enjoy expressing your strings!!

Wednesday, January 24, 2007

cross-language integration

In India, we get chewing gum named "center-shock". It looks like a popular "center-fresh" gum but when you put it in your mouth and start chewing it a very sour shot will give you an unforgettable shock.

Now if you want to test (instead of taste) this center-shock of .net ‘cross-language integration’. Then try creating a C# class and use it in a VB .net code

This is C# class:
public class MyCsClass
{
public int yogee;
public int yoGee;
public int bariable;
public int xariable;
}

Tuesday, January 16, 2007

Rounding Functions (with T-SQL)

Everybody knows floor and ceiling function.
They are same as in any other API.
Hope you already know those terms and how to use it.
And as you know floor and ceiling functions, you must be familiar the 'round' function.
Let that roam around 'round' first.
Round accepts two arguments, first as the number which you want to round and the other as to the decimal point upto you want to round.
round( input number, decimal point to round)
So input output will be as show as below:
round(136.84,2) -> 136.84
round(136.84,1) -> 136.80
round(136.84,0) -> 137.00
round(136.84,-1) -> 140.00
And you can go so on...

Indian currency is Rupee (INR). All transactions are stored in rupee.
Paise is 100th part of Rupee. And when we round money, we have to round to 25 paise or 50 paise. So simple round function shown above will never work. You have to write you own. Wait for a while , I will tell you what you can write.

Take another case. Suppose you are a shopkeeper and in order to be nice to your customer, you don't want to round merely 100.10 to 101 or 100.5. On the other hand, you cannot leave 99 paise on the a product of 97.99 rupees. So as a shop keeper, you will always love to provide rule for rounding. i.e. Round to lower digit upto 70 paise and round upward if it's more than 70 paisa.
Logic seems bigger but this can be achieved by just one multiplication, addition and devision.

For T-SQL,

declare @rnd_amt decimal(6,2)
declare @devide decimal(11,9)
set @rnd_amt = 1 -- Rounding amount.. by Rupee 1. 100 should be multiple of @rnd_amt.

-- Use any one of these three value of @devide according to you need i.e. round, floor, ceiling
set @devide = 0.00 -- Round to Floor
set @devide = 0.9999999 -- Roudn to Ceiling
set @devide = 0.70 -- Nearest Rounding. Devide. In above example it's 0.70 i.e.70% . So 70 paise or more than that will be rounded upward.
print floor(58.00001/@rnd_amt + @devide)*@rnd_amt


e.g.
print floor(58.7455698/1+.70)*1

Easy huh!!