Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am doing some string replace in SQL on the fly.

MySQLString = " a.account=b.account ";
MySQLString = " a.accountnum=b.accountnum ";

Now if I do this

MySQLString.replaceAll("account", "account_enc");

the result will be

a.account_enc=b.account_enc 

(This is good)

But look at 2nd result

a.account_enc_num=a.account_enc_num 

(This is not good it should be a.accountnum_enc=b.accountnum_enc)

Please advise how can I achieve what I want with Java String Replace.

Many Thanks.

share|improve this question
    
You should look at using a capturing group in your regex. –  Code-Apprentice Feb 23 '13 at 1:42
1  
Next time, try to explain exactly what you need better, perhaps saying: I want this, this and this to be replaced, but not this, this and this. –  Oscar Mederos Feb 23 '13 at 2:41
    
@OscarMederos apologies, will do. –  user2101374 Feb 23 '13 at 2:55

3 Answers 3

up vote 1 down vote accepted

From your comment:

Is there anyway to tell in Regex only replace a.account=b.account or a.accountnum=b.accountnum. I do not want accountname to be replace with _enc

If I understand correctly you want to add _enc part only to account or accountnum. To do this you can use

MySQLString = MySQLString.replaceAll("\\baccount(num)?\\b", "$0_enc");

(num)? mean that num is optional so regex will accept account or accountnum

\\b at start mean that there can be no letters, numbers or "_" before account so it wont accept (affect) something like myaccount, or my_account.

\\b at the end will prevent other letters, numbers or "_" after account or accountnum.

share|improve this answer
    
beat me to it ;) –  Oscar Mederos Feb 23 '13 at 2:40
    
@OscarMederos hope OP wont say now something like "but I want it to update only if two words that I am changing are identical" so no change in a.account=b.accountnum :) (I've seen that before :D) –  Pshemo Feb 23 '13 at 2:46
    
I don't think so :) –  Oscar Mederos Feb 23 '13 at 2:47
    
@Pshemo I need to try it out in Java to be sure but if \\b prevents letters or numbers or underscore before account will it catch a.account i.e. a. before account? –  user2101374 Feb 23 '13 at 2:50
1  
@user2101374 if this answer solves your problem you can [accept] it as solution to mark your problem as solved so other users wont waste their time trying to create other answers :) –  Pshemo Feb 23 '13 at 2:59

It's hard to extrapolate from so few examples, but maybe what you want is:

MySQLString = MySQLString.replaceAll("account\\w*", "$0_enc");

which will append _enc to any sequence of letters, digits, and underscores that starts with account.

share|improve this answer
    
Thank you very much Ruakh, I will give it a try, many thanks, you guys are amazing. –  user2101374 Feb 23 '13 at 1:31
    
Hi Ruakh, this will even cause accountname to be changed to accountname_enc which I do not want, any other suggestion? –  user2101374 Feb 23 '13 at 1:35
    
@user2101374: How can you tell that you want accountnum_enc but not accountname_enc? –  ruakh Feb 23 '13 at 1:40
    
Hi Ruakh, I have a list of columns in a file which I know I need to add enc. Actually enc stands for encrypted so only account number need to be changed to _enc. The column name containing account can either be called account or accountnum. –  user2101374 Feb 23 '13 at 1:46

try

    String s = " a.accountnum=b.accountnum ".replaceAll("(account[^ =]*)", "$1_enc");

it means replace any sequence characters which are not ' ' or '=' which starts the word "account" with the sequence found + "_enc".

$1 is a reference to group 1 in regex; group 1 is the expression in parenthesis (account[^ =]+), i.e. our sequence

See http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html for details

share|improve this answer
    
Thanks Evgeniy, could you please explain the regex you have used? Apologies I am not very good at Regex want to understand does the regex you have used means –  user2101374 Feb 23 '13 at 1:44
    
OK, see my update –  Evgeniy Dorofeev Feb 23 '13 at 1:55
    
Evgeniy, wouldnt this even replace a.name=b.name with a.name_enc=b.name_enc? I only like to replace a.account=b.account to be a.account_enc=b.account_enc OR a.accountnum=b.accountnum to be a.accountnum_enc=b.accountnum_enc –  user2101374 Feb 23 '13 at 1:58
    
It wont after I've fixed it :), now I think you have enough examples to make corrections to the regex. –  Evgeniy Dorofeev Feb 23 '13 at 2:01
    
It seems there is just one issue with this Regex, it will even convert a.accountname=b.accountname to a.accountname_enc=b.accountname_enc, correct? Is there anyway to tell in Regex only replace a.account=b.account or a.accountnum=b.accountnum, I do not want accountname to be replace with _enc. Thank you very much. Very much appreciated :) –  user2101374 Feb 23 '13 at 2:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.