Bluehost.com Web Hosting $6.95

Rgular Expression - 1st alternative

This is a discussion on Rgular Expression - 1st alternative within the PHP General forums, part of the PHP Programming Forums category; Hi all. I've got one problem with %subj%. I need to get 1st appearance of any alternative pattern, but ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-09-2003
\Emo
 
Posts: n/a
Default Rgular Expression - 1st alternative

Hi all.

I've got one problem with %subj%. I need to get 1st appearance of any
alternative pattern, but my regexp get last.

in $out[2] (input) is "columns FROM tables WHERE conditions ORDER BY sth
DESC LIMIT 1 OFFEST 0"

preg_match("/[[:space:]]*(.*)[[:space:]]+from[[:space:]]+(.*)[[:space:]]+whe
re[[:space:]]+(.*)[[:space:]]+(order by|group by|having|limit)(.*)/is",
$out[2], $out);

This will produce:
Array
(
[0] => columns FROM tables WHERE conditions ORDER BY sth DESC LIMIT 1
OFFEST 0
[1] => columns
[2] => tables
[3] => conditions ORDER BY sth DESC
// but here I need only "conditions"
[4] => LIMIT
// and here shloud be ORDER BY sth DESC LIMIT
[5] => 1 OFFEST 0
)

I was searchnig web and forums long time, but didn't found working advice.
I tried to use '?' and other pieces of regexp pattern, but I can't write
functional regexp.

Thanx for any advices.

Emo
Reply With Quote
  #2 (permalink)  
Old 10-09-2003
Curt Zirzow
 
Posts: n/a
Default Re: Rgular Expression - 1st alternative

On Thu, 9 Oct 2003 19:54:09 +0200, Emo <emo@emo-cz.net> wrote:

> Hi all.
>
> I've got one problem with %subj%. I need to get 1st appearance of any
> alternative pattern, but my regexp get last.


Remember .* is greedy by default you need to use .*? also instead of using
[[:space:]] I might suggest using \s, it will make it shorter

>
> in $out[2] (input) is "columns FROM tables WHERE conditions ORDER BY sth
> DESC LIMIT 1 OFFEST 0"
>
> preg_match("/[[:space:]]*(.*)[[:space:]]+from[[:space:]]+(.*)
> [[:space:]]+whe
> re[[:space:]]+(.*)[[:space:]]+(order by|group by|having|limit)(.*)/is",
> $out[2], $out);


/^\s*(.*?)\s+from\s+(.*?)\s+where\s+(.*?)\s+(order by|group
by|having|limit)(.*)/is

>
> This will produce:
> Array
> (
> [0] => columns FROM tables WHERE conditions ORDER BY sth DESC LIMIT 1
> OFFEST 0
> [1] => columns
> [2] => tables
> [3] => conditions ORDER BY sth DESC
> // but here I need only "conditions"
> [4] => LIMIT
> // and here shloud be ORDER BY sth DESC LIMIT
> [5] => 1 OFFEST 0
> )


I'm not entirely clear how you want to capture the order by|group by| etc..

Curt
--
Reply With Quote
Reply
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

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 04:27 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0