wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes More Regex Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "More Regex Help" Watch "More Regex Help" New topic
Author

More Regex Help

Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 234
    
    1

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?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

Rob Spoor wrote:contains, lastIndexOf and replace do not work with regular expressions. There are only two methods of String that do - replaceAll and matches.

<nitpick>
Four. replaceFirst(...) and (two overloads of) split(...).
</nitpick>


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

I've never used replaceFirst so that explains why I missed that, but missing split? Jeeez....
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7711
    
  20

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.

My advice:
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.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: More Regex Help