0

I have a string (say)

int t=5; 

I want to replace the identifier 't' in above string with some other String say "abc". using the replaceAll() or replace() methods of String in java replaces char 't' in int as well. so the output that i get is

"inabc abc=5;"

I want to replace the identifier only. please help me out. Thank you.

6
  • Decide exactly what you want to replace. Do you want to replace the "t" in "tin"? "+t+"? "$t$"? "alphabet"? "titan"? What if the whole string is just "t"? Java does not have a built-in library for parsing Java source code, so if you want to parse Java source code, you're in for a lot of work (or finding a library for it). Commented Mar 31, 2014 at 9:33
  • If you are trying to build tool which would let you rename your variables then you don't have to. Just use IDE like Eclipse, mark variable you want to replace (or set cursor on it) and pick Refactor -> Rename... Commented Mar 31, 2014 at 9:41
  • If you don't want to use IDE functionality then you need to create your own Java parser. Regex is not best tool for this. What if variables will have names same as some classes like String String = "foo";? Commented Mar 31, 2014 at 10:01
  • Other example of why regex would not be good for this are shadowed variables for instance int i = 1; {int i = 2; i++} Which i should be renamed? Commented Mar 31, 2014 at 10:05
  • I am actually reading a .java file and then storing all the identifiers of this .java code into a set in pass 1. And then in pass 2 i am using this set to replace the identifiers by the ceaser cipher equivalent of that identifier. Yeah i know that if the identifiers are same as the java classes name, it wont work. so could you please suggest me a parser that can actually find all the identifiers. Thnx Commented Mar 31, 2014 at 10:11

4 Answers 4

5

You need to use the word anchor (\b) as in:

s = s.replaceAll("\\bt\\b", "abc");

A regex has no notion of a word!


However, this will only ever bring you so far; this regex may fail on some more complicated constructs. You really want to use a parser if you want more complex substitutions; parboiled, for instance.

Sign up to request clarification or add additional context in comments.

Comments

1

I think this would work-

yourString.replace(" t", " abc");

Comments

0

Use the following regexp to replace all occurrences of a variable, assuming that before and after the name there cannot be any word-like character:

    String s = "int t=5";

    String s2 = s.replaceAll("(?<!\\w)t(?!\\w)", "abc");

Comments

-1

I think you can change "t=" to "abc=".

str.replace("t=", "abc=");

3 Comments

What about cases where there are one or more spaces between t and =? Or if t is last character in some other word like it=?
@Pshemo This question is only need to change the second "t" to "abc", I think this is the quick way. If there are other requirements use regex is the best way.
For this particular case, yes your solution will work, but it seems that this is only example of what OP is really trying to do, so try to add more general answer as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.