By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,468 Members | 1,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,468 IT Pros & Developers. It's quick & easy.

Regular expression problem

P: n/a
I am trying to come up with a regular expression that searches and
replaces all occurences of the string "'" and replaces it with "''" .
However if a backslash comes before the single quote I do NOT want it
too be replaced with two single quotes. Any clues?

Joe
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a


Joe Ray wrote:
I am trying to come up with a regular expression that searches and
replaces all occurences of the string "'" and replaces it with "''" .
However if a backslash comes before the single quote I do NOT want it
too be replaced with two single quotes. Any clues?

Joe


Try:

public class regexDemo {
private static void show( String s )
{
System.out.println( "IN [" + s + "]" );
s = s.replaceAll( "([^\\\\])\'", "$1\'\'" );
System.out.println( "OUT[" + s + "]\n" );

return;
}

public static void main( String args[] )
{
show( "This 'string' needs quoting." );
show( "And 'quote' some of these but don\\'t do all." );

return;
}
}

Tim

Jul 17 '05 #2

P: n/a
D
ra****@hotmail.com (Joe Ray) wrote in message news:<46*************************@posting.google.c om>...
I am trying to come up with a regular expression that searches and
replaces all occurences of the string "'" and replaces it with "''" .
However if a backslash comes before the single quote I do NOT want it
too be replaced with two single quotes. Any clues?

Joe

[^\\]'
Jul 17 '05 #3

P: n/a
Joe Ray wrote:
I am trying to come up with a regular expression that searches and
replaces all occurences of the string "'" and replaces it with "''" .
However if a backslash comes before the single quote I do NOT want it
too be replaced with two single quotes. Any clues?


A zero-width look-ahead assertion should be able to do what you need.

(?<=[^\\]|.{0})'
Jul 17 '05 #4

P: n/a
Joe Ray wrote:
I am trying to come up with a regular expression that searches and
replaces all occurences of the string "'" and replaces it with "''" .
However if a backslash comes before the single quote I do NOT want it
too be replaced with two single quotes. Any clues?

Joe


Joe,

<mind-reading-mode>
If you are doing this for the purpose of creating an SQL string, you
will be better off in the long run to use a PreparedStatement. E.g.

Connection conn = getConnection();
PreparedStatement pStmt = conn.prepareStatement(
"SELECT id FROM My_Table WHERE name = ?");
pStmt.setString(1, "Joe's Pizza");
ResultSet rs = pStmt.execute();

Then it is the JDBC driver's problem to worry about escaping strings.
</mind-reading-mode>

HTH,
Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #5

P: n/a

I do something like this for a jdbc insert:

private int insertDetailStatement() {
/**
* Buid SQL Data Statement
*/
int fKeyDetail = checkDupDetail();
if (fKeyDetail > 0) {
return fKeyDetail;
};
/**
* Build the author_detail insert STATEMENT with data
*/
String sqlStatement = DETAILINSERTSQL
+ jTitle.getText().trim().replaceAll("'", "''") + "', '"
+ jSuffix.getText().trim().replaceAll("'", "''") + "', '"
+ jLastName.getText().trim().replaceAll("'", "''") + "', '"
+ jMiddleName.getText().trim().replaceAll("'", "''") + "', '"
+ jFirsfName.getText().trim().replaceAll("'", "''") + "', '"
+ jBirthDeath.getText() + "', '"
+ jdescription.getText().trim().replaceAll("'", "''") + "', '"
+ jLink.getText() + "')";
/**
* Insert the Author Data
*/
JPeopleQuotesAppDB.insertTableRow(sqlStatement);

return checkDupDetail();
"Raymond DeCampo" <rd******@spam.twcny.spam.rr.spam.com.spam> wrote in
message news:_3*******************@twister.nyroc.rr.com...
Joe Ray wrote:
I am trying to come up with a regular expression that searches and
replaces all occurences of the string "'" and replaces it with "''" .
However if a backslash comes before the single quote I do NOT want it
too be replaced with two single quotes. Any clues?

Joe


Joe,

<mind-reading-mode>
If you are doing this for the purpose of creating an SQL string, you
will be better off in the long run to use a PreparedStatement. E.g.

Connection conn = getConnection();
PreparedStatement pStmt = conn.prepareStatement(
"SELECT id FROM My_Table WHERE name = ?");
pStmt.setString(1, "Joe's Pizza");
ResultSet rs = pStmt.execute();

Then it is the JDBC driver's problem to worry about escaping strings.
</mind-reading-mode>

HTH,
Ray

--
XML is the programmer's duct tape.

Jul 17 '05 #6

P: n/a
Chris Dutton wrote:
A zero-width look-ahead assertion should be able to do what you need.

(?<=[^\\]|.{0})'
^^^^^
Will you please explain the last part haw it works ?
Thank you

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.