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
Java

Avatar of undefined
Last Comment
Mick Barry

8/22/2022 - Mon
matthew016

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

?
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
jaipur07

ASKER
basically

date('15/06/2006')

should look like:

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

after replacement operaion
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
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");

CEHJ

Didn't see your alteration


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

ASKER CERTIFIED SOLUTION
CEHJ

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
matthew016

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);
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
matthew016

oh ok didnt see your replys cehj
matthew016

U can use also this

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


Mick Barry

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

s = s.replaceAll(")", ", 'mm/dd/yyyy')");
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
CEHJ

>>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
CEHJ

:-)
Mick Barry

> 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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CEHJ

>>less complication less chance of error.

In the case of regex, it's MORE chance of error ;-)
CEHJ

Better still would be


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

> Better still would be

already been suggested ;)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck