I want the following code to replace any apostrophes that come after a letter that is not s with an exclamation point. The program isn't even reaching the body of the if statement. What am I doing wrong?
contains, lastIndexOf and replace do not work with regular expressions. There are only two methods of String that do - replaceAll and matches. I think you'll want to use an explicit Pattern / Matcher pair here. Or, if you use a negative lookbehind, you can use replaceAll:
This negative lookbehind, (?<![sS]), will cause only apostrophes that are not preceded by an s or S to be replaced.
Joel Christophel wrote:I want the following code to replace any apostrophes that come after a letter that is not s with an exclamation point. The program isn't even reaching the body of the if statement.
Between this and your previous thread, I suspect you're tackling this problem existentially, which is not usually a good way to go.
1. Forget about regexes.
2. Turn OFF your computer.
3. Sit down and write out in English what it is you want to do.
4. When you've done that, think about scenarios that might cause problems. Again, write out solutions for them in English.
5. (Only when you're sure you've done a good job of 3 and 4) Turn your computer back on and try and code your results in Java. You may even find that regexes are NOT what you want for everything.
Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here