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.