Results 1 to 8 of 8
  1. #1
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default Using regex to replace characters in a string

    I'm trying to using the String.replace method to replace certain characters in a given string so that it can be inserted into an SQL table.

    I'd like to escape all single quotes (' to \') and backslashes (\ to \\), and get rid of all semi-colons. How would I go about doing this with regex?

    I'm horrible at regex, so I'd appreciate any help with this.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default

    I'm already trying to learn it by starting from the basics. However I find that I don't learn quite as well when I'm rushing through and looking for a solution to a problem that needs to be solved quickly.

    I've created this thread so that I can have a temporary solution in place until I'm comfortable enough with regex myself.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default

    Quote Originally Posted by DC200 View Post
    I'm trying to using the String.replace method to replace certain characters in a given string
    ok...
    so that it can be inserted into an SQL table.
    Hold it right there. Use a PreparedStatement and let the API take care of the quoting.
    I'd like to escape all single quotes (' to \') and backslashes (\ to \\), and get rid of all semi-colons.
    It does look like you're confused between the representation of a String as a literal and the actual String contents.
    How would I go about doing this with regex?
    String#replace(...) doesn't take a RegEx.
    I'm horrible at regex, so I'd appreciate any help with this.
    You've already been given links to two very good learning resources, so I have nothing more to add.

    But I repeat: use PreparedStatement.

    db
    Last edited by DarrylBurke; 10-13-2010 at 04:32 AM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default

    Quote Originally Posted by DC200 View Post
    I'm already trying to learn it by starting from the basics. However I find that I don't learn quite as well when I'm rushing through and looking for a solution to a problem that needs to be solved quickly.

    I've created this thread so that I can have a temporary solution in place until I'm comfortable enough with regex myself.
    Oh yeah, I'm sure many knowledgeable members here will want to help you pretend to have knowledge you don't have. Not.

    db

  6. #6
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default

    Thank you Grumpy, but I think I can do without the attitude.

    Anyway, prepared statements probably sound like my best bet here. They're fast, secure and neatly implemented, which is just what I prefer. Will I still be needing my regex methods, or will prepared statements eliminate the need for these?
    Last edited by DC200; 10-13-2010 at 01:18 PM.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default

    Quote Originally Posted by DC200 View Post
    Thank you Grumpy, but I think I can do without the attitude.
    Way to go, so long as you can do without the help too. Or did you think that being rude to me would bring other knowledgeable members clamoring to assist you?

    How To Ask Questions The Smart Way

    db

  8. #8
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default

    Wow, looks like someone's having a bad day. :rolleyes:

    Getting back on topic though, the prepared statements are working perfectly for data insertion. I'm not sure sure about whether or not I still need to escape some of the other special chars such as backslash, but I can probably come up with a simple regex char replacement method for that if necessary.

    Thanks for the help, everyone.

Similar Threads

  1. Regex search and replace
    By joe robles in forum Advanced Java
    Replies: 1
    Last Post: 09-25-2010, 02:32 PM
  2. How to replace typed characters?
    By Collinryans in forum New To Java
    Replies: 3
    Last Post: 05-25-2010, 03:50 AM
  3. Replace String
    By Raeghin in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 03:58 PM
  4. Regex - matching literal characters
    By racha0601 in forum Advanced Java
    Replies: 3
    Last Post: 04-07-2009, 11:25 PM
  5. string replace problem
    By soni in forum Advanced Java
    Replies: 8
    Last Post: 07-06-2008, 01:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •