TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-30-2008, 09:37 PM   #1 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default regex problem

I am making a regular expression for work to search though files and find SQL queries, it works perfectly accept it can not cater to multiple lines. How do I make this indifferent to multiple lines?
(((SELECT|DELETE) ([A-Za-z0-9-\*,`\._ ]+) FROM)|(INSERT INTO)|(UPDATE ([A-Za-z0-9-\*,`]+) SET))
__________________

Village Idiot is offline  
Reply With Quote
Old 05-30-2008, 10:51 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

You can use \n to match newlines (eg. within the character class definitions), or \v (>=5.2.4) to match any vertical whitespace.
Salathe is offline  
Reply With Quote
Old 05-30-2008, 11:02 PM   #3 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Thats for the advice, but thats not the issue. I do not know when there will be a breakline, I am searching over more than 50,000 files for what I believe will amount to 1,000 queries which where by a very inconsistent programmer.


In other words, it currently will match
Code:
SELECT `something` FROM `db` WHERE 1
But it will not match
Code:
SELECT `something` 
FROM `db'
WHERE 1
I need it to match both, no matter where the line break is
__________________

Village Idiot is offline  
Reply With Quote
Old 05-30-2008, 11:16 PM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Have you tried using the s modifier? Depending on how you're feeding the data in to your regex, it may be a quick and dirty solution to your problem (the s modifier treats any string/block that the regular expression is traversing as a single line).

Outside of that, Salathe was sending you in the right direction. You can use \r, \n or \v to check for the possibility of newlines or vertical whitespace, just the same as you would use 0-9 to check for the letters 0-9, or _ to check for underscores, etc... you just have to incorporate them into the regex whereever you think there could eventually be one. Give it the option to find it. For example,

PHP Code:
$szTest = <<<EOF
/* this
is the
test */

booga!

/* more
new lines
in this
comment
*/
EOF;

$szTest preg_replace("~/\*(.*?\r?\n?)+\*/~"'booga!'$test);
echo 
$szTest
Outputs:

booga! booga! booga!
-m
delayedinsanity is offline  
Reply With Quote
Old 05-30-2008, 11:27 PM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Adding the ability to have whitespace in the two character classes, combined with using the x modifier, would help. In order to match line breaks you have to tell the regex engine where to expect them, there's no magic allow/disallow switch.

Code:
/(
    ((SELECT|DELETE) [A-Za-z0-9-\*,`._\s]+ FROM)
    |
    (INSERT INTO)
    |
    (UPDATE [A-Za-z0-9-\*,`._\s]+ SET)
)/x
Salathe is offline  
Reply With Quote
Old 05-30-2008, 11:31 PM   #6 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Thanks guys, I'll figure a way out when Monday comes around .
__________________

Village Idiot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:38 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design