I am new to java but not programming in general. I've been trying to understand Java String replaceAll...specifically I am reading in Strings from a text file...an example would be "I JUMP UP HIGH IN THE AIR TO GET TO YOU."
1) I want to change "I" to "A" where I is not the beginning of a word, and 2) U to "O" where U is at the end of a word. Any help would be appreciated. (Also, if you can point me to a good tutorial on the topic [I learn best by looking at examples] that would be appreciated)
-
regular-expressions.info/tutorial.htmlshmosel– shmosel2016年03月07日 03:20:19 +00:00Commented Mar 7, 2016 at 3:20
1 Answer 1
Try this.
String s = "I JUMP UP HIGH IN THE AIR TO GET TO YOU.";
s = s.replaceAll("(?!\\b)I", "A")
.replaceAll("U\\b", "O");
System.out.println(s);
// -> I JUMP UP HAGH IN THE AAR TO GET TO YOO.
answered Mar 7, 2016 at 3:47
user4910279
Sign up to request clarification or add additional context in comments.
2 Comments
Hedgebox
Thank you so much...This was perfect. I have 2 more questions...How would I do a conditional replaceAll...i.e. replace all "P" at end of word unless "UP"...I looked at the tutorial shmosel provided the link to...but I didn't get it...I could really use a tutorial that shows examples if you know of one please provide the link to it....
Hedgebox
This what I tried based on your example for "U" at the end of the word.....s = s.replaceAll("(!UP)P\\b", "PS")
lang-java