Link to home
Start Free TrialLog in
Avatar of jaipur07
jaipur07

asked on

Replacing string?

Hi Guys

I have several strings like:

date('15/06/2006')
date('15/08/2001')
date('15/06/2006')
date('15/09/2002')

I want to replace '15/06/2006' part of the String with '15/06/2006' ,'mm/dd/yyyy'

how can i achieve in java?? may be using regular expressions?  or other way
Avatar of matthew016
matthew016
Flag of Belgium image

>> replace '15/06/2006' part of the String with '15/06/2006'

?
Avatar of jaipur07
jaipur07

ASKER

I want to replace >>> '15/06/2006'  <<< part of the String with >>>'15/06/2006' ,'mm/dd/yyyy'<<<

means I want to append >>> ,'mm/dd/yyyy'  to '15/06/2006' part of the string
basically

date('15/06/2006')

should look like:

date('15/06/2006' , 'dd/mm/yyyy')

after replacement operaion
Avatar of CEHJ
>>I want to replace '15/06/2006' part of the String with '15/06/2006' ,'mm/dd/yyyy'

Try

String s = "date('15/06/2006')";
s = s.replaceAll("\\d{2}/\\d{2}/\\d{4}", "mm/dd/yyyy");

Didn't see your alteration


s = s.replaceAll("('\\d{2}/\\d{2}/\\d{4}')", "$1, 'mm/dd/yyyy");

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
Not sure I understood u, but try this


            String date = "date('15/06/2006')";
            
            String[] s2 = date.split("'");
            String[] s3 = s2[1].split("/");
            
            String result = s2[0]+"'"+s3[1]+"/"+s3[0]+"/"+s3[2]+"','mm/dd/yyyy'"+s2[2];
            
            System.out.println(result);
            
            String result2 = s2[0]+"'"+s2[1]+"'"+",'mm/dd/yyyy'"+s2[2];
            
            System.out.println(result2);
oh ok didnt see your replys cehj
U can use also this

String[] s = date.split("'");
String result = s[0]+"'"+s[1]+"'"+",'mm/dd/yyyy'"+s[2];


no need to overcomplicate it :) this'll do the job:

s = s.replaceAll(")", ", 'mm/dd/yyyy')");
>>no need to overcomplicate it :)

It's nothing to do with complication ;-) REs should be as tight as possible or you may find them matching things you didn't want them to match
:-)
> It's nothing to do with complication ;-)

It absolutely is, less complication less chance of error.

> REs should be as tight as possible or you may find them matching things you didn't want them to match

In that case then you wouldn't use what you suggested
>>less complication less chance of error.

In the case of regex, it's MORE chance of error ;-)
Better still would be


s = s.replaceAll("(date\\('\\d{2}/\\d{2}/\\d{4}')", "$1, 'mm/dd/yyyy'");
> Better still would be

already been suggested ;)