Link to home
Start Free TrialLog in
Avatar of Amit
AmitFlag for United States of America

asked on

help with a regular expression

Hi,

I am passing the following argument
dateheader = "09/24/2009 15:16:32.099";
to the regular expression below

strsecond =  String.valueOf(dateheader.replaceAll(".+[\\ ]+([0-9]{1,2}):([0-9]*):([0-9]*):(.+)","$3"));

so that I can get the seconds in this case the answer should be 32. But I am getting the following output

09/24/2009 15:26:33.309

Could someone please fix the regular expression for me.

Also is there another cleaner way to handle dates. Like get the date part, year part , etc
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>Also is there another cleaner way to handle dates. Like get the date part, year part , etc

Yes. Use a DateFormat to set the Date on a Calendar
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(and then of course your seconds are atoms[atoms.length - 2]
Avatar of Amit

ASKER

Hi CEHJ,

Could you please give me the whole regex. While I explore the datepart funcitonality. I don't know regex and I got this as maintenance work :-(

thanks
-anshu
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm really unclear as to why you want to replaceAll - it's the worst solution, but:

dateheader = dateheader(".*:(\\d+).*", "$1");
Oops

dateheader = dateheader.replaceAll(".*:(\\d+).*", "$1");
Avatar of Amit

ASKER

both of you are god
:-)