Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 26th, 2012, 12:44 AM
DrWorm's Avatar
DrWorm DrWorm is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2002
Location: Queensland, Australia
Posts: 826 DrWorm User rank is First Lieutenant (10000 - 20000 Reputation Level)DrWorm User rank is First Lieutenant (10000 - 20000 Reputation Level)DrWorm User rank is First Lieutenant (10000 - 20000 Reputation Level)DrWorm User rank is First Lieutenant (10000 - 20000 Reputation Level)DrWorm User rank is First Lieutenant (10000 - 20000 Reputation Level)DrWorm User rank is First Lieutenant (10000 - 20000 Reputation Level)DrWorm User rank is First Lieutenant (10000 - 20000 Reputation Level)DrWorm User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 5 Days 17 h 51 m 49 sec
Reputation Power: 140
Match string not surround by quotes

I need to do a string replacement within an SQL statement but only where the the pattern does not occur within a text string. This is to compensate for PHP PDOs lack of native functions for construction a CSV of placeholder in an IN statement.

So I might have a query like:
SELECT * FROM table where colId IN(XX).

I would then search for "XX" and replace it with a generated string to produce
SELECT * FROM table WHERE colId IN(?, ?, ?)

That part is easy.

What I want to avoid is:
SELECT * from table WHERE colDesc "Hello XX World"
becoming
SELECT * FROM table WHERE colDesc "Hello ?, ?, ? World"
__________________
Ooh, they have the Internet on computers now!

Reply With Quote
  #2  
Old October 27th, 2012, 06:09 AM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 508 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 27 m 4 sec
Reputation Power: 385
Well, the better you know about your data, the better you are off with regexes in general. We would need to know more on how hairy your SQL select statements can be: for example, can there be several quoted strings or only one? If yes, you could do the substitution only if the line does not match /".*XX.*"/.

Another possibility: if the "XX" that you want to replace is always part of the string "IN (XX)", you could have something like:

Code:
/IN\s*\(XX\)/

Reply With Quote
  #3  
Old October 27th, 2012, 07:47 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,931 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 7 h 49 m 26 sec
Reputation Power: 7053
If you're using bound parameters you should never end up with arbitrary strings in your SQL query, so you shouldn't run into an issue as you described.

There is really no way to use a reg ex to figure out whether "xx" is a placeholder in the sql query or part of a string.
__________________
PHP FAQ
How to program a basic, secure login system using PHP

Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #4  
Old October 27th, 2012, 08:36 PM
DrWorm's Avatar
DrWorm DrWorm is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2002
Location: Queensland, Australia
Posts: 826 DrWorm User rank is First Lieutenant (10000 - 20000 Reputation Level)DrWorm User rank is First Lieutenant (10000 - 20000 Reputation Level)DrWorm User rank is First Lieutenant (10000 - 20000 Reputation Level)DrWorm User rank is First Lieutenant (10000 - 20000 Reputation Level)DrWorm User rank is First Lieutenant (10000 - 20000 Reputation Level)DrWorm User rank is First Lieutenant (10000 - 20000 Reputation Level)DrWorm User rank is First Lieutenant (10000 - 20000 Reputation Level)DrWorm User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 5 Days 17 h 51 m 49 sec
Reputation Power: 140
Quote:
Originally Posted by E-Oreo
If you're using bound parameters you should never end up with arbitrary strings in your SQL query, so you shouldn't run into an issue as you described.

You're right, as a rule of thumb there shouldn't be an existing string in the query.

I thought if I could catch it, I would. But if I can't, well the query will fall over when executed anyway, at which point the programmer should remove the string or escape the placeholder.

Thanks for the responses.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Match string not surround by quotes

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap