Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular Expression Help Needed

 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String str = "L''ORE'ALX";
str.replace("'","''"); will give ooutput like L''''ORE''ALX which i dont need .

select * from Table where column like 'L''ORE'ALX%' / it will throw bad sql grammars exception since we have single quote ( ' ) needs to relapced to double quote( '') select * from Table where column like 'L''ORE''ALX%'

so i need only convert the ( ' ) to ( '') which is in between E and A ,so that final string will be
'L''ORE''ALX'

so in regular exp i need to search with pattern like single quotes between any charactres so that i can replace to ( '') quotes .

pls do help .
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks

I came up with some code which is apt for above example but needs refactoring


Please comment or suggest me
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before suggesting you how you can do the replacement, could you tell what the result of the replacement action should look like? Seems to have something to do with escaping Strings for SQL statements, but what String do you actually want to write to the database, i.e. what should the String look like if you read it back from the database?
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation, though sending it as a private message is not the way to go ... it will make it harder for later readers to follow this thread.

As for your question, I'd try it in two steps:
1. escape all the ' characters

2. unescape the wrongfully escaped ones

The second replace operation will undo the first where necessary. I'm pretty sure this can be done in one step with a more sophisticated regular expression, but this way is probably the easiest and also quickest, since matching will be pretty fast with such simple patterns.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this is being done for SQL, it may be a good idea to use a PreparedStatement instead. Then you don't have to worry about escapes at all - you just call setString(), and escapes are all handled by the driver.

The original input string L''ORE'ALX doesnt' make much sense to me. Is this a case where someone already escaped the first ' correctly, but failed to escape the last one? So we're just looking for things that didn't already get escaped correctly?

Guido's two-step approach seems to make sense. Another option is to replace ' only if there is no other ' before or after:

Here (?<!') means from this position, you cannot look backwards and see a ', while (?!') means from this position you cannot look forwards and see a '. In between is the single ' that you do want to see. This should work, though Guido's approach may well be easier to read.
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim and Guido

Thanks for your reply pls see my code

please help me
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi JIm and Gildo

All i need is the regular expression which finds the pattern char/digit'char/digit ( example a'9 or 9'9 so on )
and then i need to replace the pattern with if single quote then double quote

please help
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, I should have said replaceAll() in my post, rather than replace().
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,

Thanks a lot for your kind help .

i did not notice that i have one method in String which takes regular expression as input .
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would very strongly second the suggestion to use a PreparedStatement. It saves you a lot of trouble dealing with special characters *and* is likely to lead to slightly better performance *and* typically makes the code easier to read and maintain, in my experience.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic