Thursday, June 5, 2008
Regular Expressions in Java
Regular Expressions in Java:
A regular expression works by matching a String against a template or pattern (a Pattern object in Java), and in its simplest form, returning a boolean to say "yes, the string does look like the pattern" or "no, that doesn't match".
As of release 1.4 of Java, though, there's a standard package
java.util.regex
that's shipped with the JRE, and that's what we'll look at in this module.
"^\\S+@\\S+$"
is a regular expression to see if the string entered is an e-mail
Ex:
Pattern email = Pattern.compile("^\\S+cat\\S+$");
Matcher match=email.matcher("vandicated");
if(match.matches()){
System.out.println("Matches vindicated pattern");
}else{
System.out.println("No Match found");
}
If you want to match at the start of a String, start your pattern with a ^ character; if you want to match at the end, conclude your pattern with a $ character. Should you specify both a ^ and a $, then you're looking to match the complete String to your regular expression.
The ^ and $ elements are known as "anchors" as they tie the start and/or the end of the String down; this group as a whole is also known as "assertions" because they don't match any specific characters in the incoming string, they just assert that while the match is running a certain condition must occur at the given point in the match.
If you write
[abcdef]
in your regular expression, then you're matching any one character from the list given (a b c d e or f). You can expand this capability further by using a minus sign to specify a character range, thus
[a-z]
any lower case letter
[0-9a-fA-F]
any hexadecimal character
and if you want to match any character except one from a list, you can start the character list with an ^ character, for example:
[^a-z]
any character except a lower case letter
[^%0-9]
any character except a digit or a % character
but that would get messy really fast, so there are some common groupings available in Java's regular expressions:
\s
any white space character
\d
any digit
\w
any word character (letter, digit, underscore)
If you want any character except one of these, use a capital letter:
\S
any character that is not a white space
\D
any character that is not a digit
\W
any character that is not a word character
Sequences such as \s will be familiar to you if you use Perl's regular expressions, but there are other character groups too; these use a POSIX standard definition of the character groups, but it's extended and the format isn't taken from Perl, nor PHP, nor Tcl nor SQL!
\p{Space}
Alternative to \s for "any white space"
\p{Blank}
Space or tab character
\p{Alpha}
Any letter (upper or lower case)
\p{Graph}
Any visible character
\p{InGreek}
Any Greek letter
\p{Sc}
A currency symbol
You can negate these groups using \P rather than \p thus
\P{Graph}
Any character that is not visible
One final grouping, the ultimate group if you like, is the "." (full stop or period) character, which matches virtually any character.
COUNTS
The fourth main group (after anchors, literal characters, and character groups) are the counts; you use these in regular expressions if you want to give a quantity to a literal character or group, and you add the count character into you pattern directly after the element to which it applies. There are three very common counts:
+
one or more
*
zero or more
?
zero or one
Source: http://www.wellho.net/solutions/java-regular-expressions-in-java.html
Fun exploring new things. :)
A regular expression works by matching a String against a template or pattern (a Pattern object in Java), and in its simplest form, returning a boolean to say "yes, the string does look like the pattern" or "no, that doesn't match".
As of release 1.4 of Java, though, there's a standard package
java.util.regex
that's shipped with the JRE, and that's what we'll look at in this module.
"^\\S+@\\S+$"
is a regular expression to see if the string entered is an e-mail
Ex:
Pattern email = Pattern.compile("^\\S+cat\\S+$");
Matcher match=email.matcher("vandicated");
if(match.matches()){
System.out.println("Matches vindicated pattern");
}else{
System.out.println("No Match found");
}
If you want to match at the start of a String, start your pattern with a ^ character; if you want to match at the end, conclude your pattern with a $ character. Should you specify both a ^ and a $, then you're looking to match the complete String to your regular expression.
The ^ and $ elements are known as "anchors" as they tie the start and/or the end of the String down; this group as a whole is also known as "assertions" because they don't match any specific characters in the incoming string, they just assert that while the match is running a certain condition must occur at the given point in the match.
If you write
[abcdef]
in your regular expression, then you're matching any one character from the list given (a b c d e or f). You can expand this capability further by using a minus sign to specify a character range, thus
[a-z]
any lower case letter
[0-9a-fA-F]
any hexadecimal character
and if you want to match any character except one from a list, you can start the character list with an ^ character, for example:
[^a-z]
any character except a lower case letter
[^%0-9]
any character except a digit or a % character
but that would get messy really fast, so there are some common groupings available in Java's regular expressions:
\s
any white space character
\d
any digit
\w
any word character (letter, digit, underscore)
If you want any character except one of these, use a capital letter:
\S
any character that is not a white space
\D
any character that is not a digit
\W
any character that is not a word character
Sequences such as \s will be familiar to you if you use Perl's regular expressions, but there are other character groups too; these use a POSIX standard definition of the character groups, but it's extended and the format isn't taken from Perl, nor PHP, nor Tcl nor SQL!
\p{Space}
Alternative to \s for "any white space"
\p{Blank}
Space or tab character
\p{Alpha}
Any letter (upper or lower case)
\p{Graph}
Any visible character
\p{InGreek}
Any Greek letter
\p{Sc}
A currency symbol
You can negate these groups using \P rather than \p thus
\P{Graph}
Any character that is not visible
One final grouping, the ultimate group if you like, is the "." (full stop or period) character, which matches virtually any character.
COUNTS
The fourth main group (after anchors, literal characters, and character groups) are the counts; you use these in regular expressions if you want to give a quantity to a literal character or group, and you add the count character into you pattern directly after the element to which it applies. There are three very common counts:
+
one or more
*
zero or more
?
zero or one
Source: http://www.wellho.net/solutions/java-regular-expressions-in-java.html
Fun exploring new things. :)
Wednesday, May 21, 2008
Is There Any Reason To Get An MBA
Expert views on MBA:
Found this interesting article on Forbes.com
Arthur Blank
Education is a lifetime process. This is just one step along the way.
Tim Blixseth
That is one approach to success, but most people I know with a substantial net worth do not have one.
Otis Booth
Yes, many. Teaches the conventions by which businesses are conducted
Mark Cuban
Only if someone else is paying for it and you are going to a school where you can have a lot of fun.
Gerald Ford
Probably.
Danny Gilbert
What's an "MBA"?
Kenneth Hendricks
I don't know of any reason. I believe you can be successful without an MBA (i.e. Ted Turner and Bill Gates).
Wayne Huizenga
Yes, education is the most important thing to obtain the edge.
George Kaiser
Not for entrepreneurial business
Michael Ilitch
I don't have one myself, but I think it's a good idea today for people to get them. There's plenty to learn both in school and out. Always be open to learning.
William Moncrief
No.
Phillip Ruffin
It can't hurt but it's not essential.
Jorge Perez
Never got one, so I would not know. Nevertheless, the better the educational credentials, the greater the chance of getting a better start and the foundation to allow you to succeed.
James Sorensen
Yes, it would be nice to have the information it provides, but that is only the beginning. The real degree is earned after the formal degree.
Nice thoughts... and questions to ask yourself :)
-Nita
Found this interesting article on Forbes.com
Arthur Blank
Education is a lifetime process. This is just one step along the way.
Tim Blixseth
That is one approach to success, but most people I know with a substantial net worth do not have one.
Otis Booth
Yes, many. Teaches the conventions by which businesses are conducted
Mark Cuban
Only if someone else is paying for it and you are going to a school where you can have a lot of fun.
Gerald Ford
Probably.
Danny Gilbert
What's an "MBA"?
Kenneth Hendricks
I don't know of any reason. I believe you can be successful without an MBA (i.e. Ted Turner and Bill Gates).
Wayne Huizenga
Yes, education is the most important thing to obtain the edge.
George Kaiser
Not for entrepreneurial business
Michael Ilitch
I don't have one myself, but I think it's a good idea today for people to get them. There's plenty to learn both in school and out. Always be open to learning.
William Moncrief
No.
Phillip Ruffin
It can't hurt but it's not essential.
Jorge Perez
Never got one, so I would not know. Nevertheless, the better the educational credentials, the greater the chance of getting a better start and the foundation to allow you to succeed.
James Sorensen
Yes, it would be nice to have the information it provides, but that is only the beginning. The real degree is earned after the formal degree.
Nice thoughts... and questions to ask yourself :)
-Nita
Tuesday, May 6, 2008
Spring Batch Update Code
Code snippet to execute batch operations using spring
Collection vBatchUtilitySlotModelCollection = batchSlotUtilityBusinessService.getCollection();
if(vBatchUtilitySlotModelCollection != null && vBatchUtilitySlotModelCollection.size()>0)
{
BatchSqlUpdate batchUpdater = new BatchSqlUpdate(getDataSource(),"UPDATE slot SET location_seq_num = ?, facility_seq_num = ? WHERE seq_num = ?");
batchUpdater.declareParameter(new SqlParameter(Types.BIGINT));
batchUpdater.declareParameter(new SqlParameter(Types.BIGINT));
batchUpdater.declareParameter(new SqlParameter(Types.BIGINT));
batchUpdater.compile();
for (Iterator iter = vBatchUtilitySlotModelCollection.iterator(); iter.hasNext(); ) {
VBatchUtilitySlotModel vBatchUtilitySlotModel = (VBatchUtilitySlotModel)iter.next();
batchUpdater.update(new Object[] {vBatchUtilitySlotModel.getLocationSeqNum(), vBatchUtilitySlotModel.getFacilitySeqNum(), vBatchUtilitySlotModel.getSeqNum()});
}
batchUpdater.flush();
}
It worked like a flash..!!!!
-Happy Coding
--Sunny
Collection vBatchUtilitySlotModelCollection = batchSlotUtilityBusinessService.getCollection();
if(vBatchUtilitySlotModelCollection != null && vBatchUtilitySlotModelCollection.size()>0)
{
BatchSqlUpdate batchUpdater = new BatchSqlUpdate(getDataSource(),"UPDATE slot SET location_seq_num = ?, facility_seq_num = ? WHERE seq_num = ?");
batchUpdater.declareParameter(new SqlParameter(Types.BIGINT));
batchUpdater.declareParameter(new SqlParameter(Types.BIGINT));
batchUpdater.declareParameter(new SqlParameter(Types.BIGINT));
batchUpdater.compile();
for (Iterator iter = vBatchUtilitySlotModelCollection.iterator(); iter.hasNext(); ) {
VBatchUtilitySlotModel vBatchUtilitySlotModel = (VBatchUtilitySlotModel)iter.next();
batchUpdater.update(new Object[] {vBatchUtilitySlotModel.getLocationSeqNum(), vBatchUtilitySlotModel.getFacilitySeqNum(), vBatchUtilitySlotModel.getSeqNum()});
}
batchUpdater.flush();
}
It worked like a flash..!!!!
-Happy Coding
--Sunny
Friday, May 2, 2008
Singleton across JVM's
When you make an Object singleton, we all know its just unique per jvm or per class loader.
So what if you want to make an object unique in a clustered environment. One way of doing it is using JNDI object binding. Here is some sample code try to run on ur server.
private void jndiObjTest(){
try{
System.out.println("Teting jndi object cache");
NotchClient nc=new NotchClient("Sunny","T");
Hashtable ht = new Hashtable();
ht.put(Context.PROVIDER_URL, "http://localhost:7001");
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.REFERRAL,"throw");
InitialContext ctx = new InitialContext(ht);
ctx.bind("sam_name",nc);
System.out.println("Reading Object from JNDI lookup");
Object o=ctx.lookup("sam_name");
NotchClient n=(NotchClient)o;
System.out.println("Value of obj retrieved from jndi"+n.getName());
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
Note: Please deploy the above code to ur local server to test, standalone java app wont work.
--Sunny
So what if you want to make an object unique in a clustered environment. One way of doing it is using JNDI object binding. Here is some sample code try to run on ur server.
private void jndiObjTest(){
try{
System.out.println("Teting jndi object cache");
NotchClient nc=new NotchClient("Sunny","T");
Hashtable ht = new Hashtable();
ht.put(Context.PROVIDER_URL, "http://localhost:7001");
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.REFERRAL,"throw");
InitialContext ctx = new InitialContext(ht);
ctx.bind("sam_name",nc);
System.out.println("Reading Object from JNDI lookup");
Object o=ctx.lookup("sam_name");
NotchClient n=(NotchClient)o;
System.out.println("Value of obj retrieved from jndi"+n.getName());
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
Note: Please deploy the above code to ur local server to test, standalone java app wont work.
--Sunny
Monday, April 28, 2008
Recursion in Java
public void handleParent(int parentOid){
List clientBVOList=this.getClients(parentOid);
for(int i=0;i Lessthan clientBVOList.size();i++){
NcClient nc=(NcClient)clientBVOList.get(i);
if(nc.getNotchParentFl().equalsIgnoreCase("T")){
handleParent(parentOid-1);
resetFacs(nc);
}else{
resetFacs(nc);
}
}
}
private void resetFacs(NcClient nc) {
// TODO Auto-generated method stub
System.out.println("Resetting facilities for Client:"+nc.getName());
}
private List getClients(int parentOid) {
// TODO Auto-generated method stub
List ncList=new ArrayList();
if(parentOid==3){
System.out.println("Get clients for Parent 3");
NcClient nc=new NcClient("client1","F");
NcClient nc2=new NcClient("Client2","T");
NcClient nc3=new NcClient("Client3","F");
ncList.add(nc);
ncList.add(nc2);
ncList.add(nc3);
return ncList;
}
if(parentOid==2){
System.out.println("Get clients for Parent 2");
NcClient nc=new NcClient("client2.1","F");
NcClient nc2=new NcClient("Client2.2","T");
ncList.add(nc);
ncList.add(nc2);
return ncList;
}
if(parentOid==1){
System.out.println("Get clients for Parent 1");
NcClient nc =new NcClient("Client2.2.1","F");
NcClient nc2=new NcClient("Client2.2.2","F");
ncList.add(nc);
ncList.add(nc2);
return ncList;
}
return null;
}
NC Client:
public class NClient {
String nParentFl;
String name;
public NClient(String name,String parentFl){
this.name=name;
this.nParentFl=parentFl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNParentFl() {
return nParentFl;
}
public void setNParentFl(String nParentFl) {
this.nParentFl = nParentFl;
}
}
Result:
Get clients for Parent 3
Resetting facilities for Client:client1
Get clients for Parent 2
Resetting facilities for Client:client2.1
Get clients for Parent 1
Resetting facilities for Client:Client2.2.1
Resetting facilities for Client:Client2.2.2
Resetting facilities for Client:Client2.2
Resetting facilities for Client:Client2
Resetting facilities for Client:Client3
List clientBVOList=this.getClients(parentOid);
for(int i=0;i Lessthan clientBVOList.size();i++){
NcClient nc=(NcClient)clientBVOList.get(i);
if(nc.getNotchParentFl().equalsIgnoreCase("T")){
handleParent(parentOid-1);
resetFacs(nc);
}else{
resetFacs(nc);
}
}
}
private void resetFacs(NcClient nc) {
// TODO Auto-generated method stub
System.out.println("Resetting facilities for Client:"+nc.getName());
}
private List getClients(int parentOid) {
// TODO Auto-generated method stub
List ncList=new ArrayList();
if(parentOid==3){
System.out.println("Get clients for Parent 3");
NcClient nc=new NcClient("client1","F");
NcClient nc2=new NcClient("Client2","T");
NcClient nc3=new NcClient("Client3","F");
ncList.add(nc);
ncList.add(nc2);
ncList.add(nc3);
return ncList;
}
if(parentOid==2){
System.out.println("Get clients for Parent 2");
NcClient nc=new NcClient("client2.1","F");
NcClient nc2=new NcClient("Client2.2","T");
ncList.add(nc);
ncList.add(nc2);
return ncList;
}
if(parentOid==1){
System.out.println("Get clients for Parent 1");
NcClient nc =new NcClient("Client2.2.1","F");
NcClient nc2=new NcClient("Client2.2.2","F");
ncList.add(nc);
ncList.add(nc2);
return ncList;
}
return null;
}
NC Client:
public class NClient {
String nParentFl;
String name;
public NClient(String name,String parentFl){
this.name=name;
this.nParentFl=parentFl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNParentFl() {
return nParentFl;
}
public void setNParentFl(String nParentFl) {
this.nParentFl = nParentFl;
}
}
Result:
Get clients for Parent 3
Resetting facilities for Client:client1
Get clients for Parent 2
Resetting facilities for Client:client2.1
Get clients for Parent 1
Resetting facilities for Client:Client2.2.1
Resetting facilities for Client:Client2.2.2
Resetting facilities for Client:Client2.2
Resetting facilities for Client:Client2
Resetting facilities for Client:Client3
Friday, April 25, 2008
So Small Lyrics
Carrie Underwood So Small Lyrics
While working lost in a busy day.. this song came from ipod like a refreshing spring shower ...quickly looked up lyrics on the internet..and pasting here. Another reason why I love my ipod so much.. :)).. sometimes it brings me out of my lostness.
Yeah, Yeah
What you got if you ain't got love
the kind that you just want to give away
its okay to open up
go ahead and let the light shine through
I know it's hard on a rainy day
you want to shut the world out and just be left alone
but don't run out on your faith
'cause sometimes that mountain you've been climbing
is just a grain of sand
and what you've been up there searching for forever
is in your hands
when you figure out love is all that matters after all
it sure makes everything else seem so small
it's so easy to get lost inside
a problem that seems so big at the time
it's like a river thats so wide
it swallows you whole
while you siting 'round thinking 'bout what you can't change
and worrying about all the wrong things
time's flying by
moving so fast
you better make it count 'cause you cant get it back
sometimes that mountain you've been climbing
is just a grain of sand
and what you've been up there searching for forever
is in your hands
oh when you figure out love is all that matters after all
it sure makes everything else seem so small
sometimes that mountain you've been climbing
is just a grain of sand
and what you've been up there searching for forever
is in your hands
oh when you figure out love is all that matters after all
it sure makes everything else...
oh it sure makes everything else seem so small
Yeah, Yeah
-- Playin the song agin. :D
While working lost in a busy day.. this song came from ipod like a refreshing spring shower ...quickly looked up lyrics on the internet..and pasting here. Another reason why I love my ipod so much.. :)).. sometimes it brings me out of my lostness.
Yeah, Yeah
What you got if you ain't got love
the kind that you just want to give away
its okay to open up
go ahead and let the light shine through
I know it's hard on a rainy day
you want to shut the world out and just be left alone
but don't run out on your faith
'cause sometimes that mountain you've been climbing
is just a grain of sand
and what you've been up there searching for forever
is in your hands
when you figure out love is all that matters after all
it sure makes everything else seem so small
it's so easy to get lost inside
a problem that seems so big at the time
it's like a river thats so wide
it swallows you whole
while you siting 'round thinking 'bout what you can't change
and worrying about all the wrong things
time's flying by
moving so fast
you better make it count 'cause you cant get it back
sometimes that mountain you've been climbing
is just a grain of sand
and what you've been up there searching for forever
is in your hands
oh when you figure out love is all that matters after all
it sure makes everything else seem so small
sometimes that mountain you've been climbing
is just a grain of sand
and what you've been up there searching for forever
is in your hands
oh when you figure out love is all that matters after all
it sure makes everything else...
oh it sure makes everything else seem so small
Yeah, Yeah
-- Playin the song agin. :D
Subscribe to:
Posts (Atom)