dbTalk Databases Forums  

Non-greedy wildcard?

comp.databases.mysql comp.databases.mysql


Discuss Non-greedy wildcard? in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
jwcarlton
 
Posts: n/a

Default Non-greedy wildcard? - 03-24-2011 , 06:10 PM






I've been trying to find this, but I'm not sure that I'm using the
right terminology. Sorry if this is an FAQ.

I'm trying to SELECT something using a non-greedy wildcard. I know to
use % for a greedy wildcard, but how would I correctly do the
following in MySQL?

%<img(.*?)src="*(\s*)<a%

Reading it out, I'm searching for anything, followed by <img, followed
by any character (or no character) preceding src=, followed by " or no
", followed by a whitespace or no whitespace, followed by <a, followed
by anything else.

TIA,

Jason

Reply With Quote
  #2  
Old   
onedbguru
 
Posts: n/a

Default Re: Non-greedy wildcard? - 03-24-2011 , 06:23 PM






On Mar 24, 8:10*pm, jwcarlton <jwcarl... (AT) gmail (DOT) com> wrote:
Quote:
I've been trying to find this, but I'm not sure that I'm using the
right terminology. Sorry if this is an FAQ.

I'm trying to SELECT something using a non-greedy wildcard. I know to
use % for a greedy wildcard, but how would I correctly do the
following in MySQL?

%<img(.*?)src="*(\s*)<a%

Reading it out, I'm searching for anything, followed by <img, followed
by any character (or no character) preceding src=, followed by " or no
", followed by a whitespace or no whitespace, followed by <a, followed
by anything else.

TIA,

Jason
You mean - use a regexp??

http://dev.mysql.com/doc/refman/5.1/en/regexp.html

select * from table where col1 REGEXP '%<img(.*?)src="*(\s*)<a%';

Reply With Quote
  #3  
Old   
jwcarlton
 
Posts: n/a

Default Re: Non-greedy wildcard? - 03-24-2011 , 06:47 PM



Quote:
*You mean - use a regexp??

http://dev.mysql.com/doc/refman/5.1/en/regexp.html

select * from table where col1 REGEXP '%<img(.*?)src="*(\s*)<a%';
HA! I should have known it would be something as simple as that.

I'm getting an error using that, but I'm sure I'll figure it out. The
error is:

'repetition-operator operand invalid' from regexp

Reply With Quote
  #4  
Old   
jwcarlton
 
Posts: n/a

Default Re: Non-greedy wildcard? - 03-24-2011 , 07:09 PM



On Mar 24, 8:47*pm, jwcarlton <jwcarl... (AT) gmail (DOT) com> wrote:
Quote:
*You mean - use a regexp??

http://dev.mysql.com/doc/refman/5.1/en/regexp.html

select * from table where col1 REGEXP '%<img(.*?)src="*(\s*)<a%';

HA! I should have known it would be something as simple as that.

I'm getting an error using that, but I'm sure I'll figure it out. The
error is:

'repetition-operator operand invalid' from regexp
Narrowing it down, I'm still getting the same error on this:

SELECT * FROM table WHERE col1 REGEXP 'img(.*?)'

The "repetition-operator" would have to be the *, right? According to
the dev.mysql.com link above, this is valid?

Reply With Quote
  #5  
Old   
jwcarlton
 
Posts: n/a

Default Re: Non-greedy wildcard? - 03-25-2011 , 02:08 AM



On Mar 24, 9:09*pm, jwcarlton <jwcarl... (AT) gmail (DOT) com> wrote:
Quote:
On Mar 24, 8:47*pm, jwcarlton <jwcarl... (AT) gmail (DOT) com> wrote:

*You mean - use a regexp??

http://dev.mysql.com/doc/refman/5.1/en/regexp.html

select * from table where col1 REGEXP '%<img(.*?)src="*(\s*)<a%';

HA! I should have known it would be something as simple as that.

I'm getting an error using that, but I'm sure I'll figure it out. The
error is:

'repetition-operator operand invalid' from regexp

Narrowing it down, I'm still getting the same error on this:

SELECT * FROM table WHERE col1 REGEXP 'img(.*?)'

The "repetition-operator" would have to be the *, right? According to
the dev.mysql.com link above, this is valid?
For anyone else reading this, the ? apparently doesn't translate to
"not greedy" in MySQL; I removed it, and the regex worked fine. So,
while REGEXP did work for this particular application, I'm assuming
that it's still greedy, so it doesn't exactly work in all applications.

Reply With Quote
  #6  
Old   
Álvaro G. Vicario
 
Posts: n/a

Default Re: Non-greedy wildcard? - 03-25-2011 , 03:10 AM



El 25/03/2011 9:08, jwcarlton escribió/wrote:
Quote:
On Mar 24, 9:09 pm, jwcarlton<jwcarl... (AT) gmail (DOT) com> wrote:
On Mar 24, 8:47 pm, jwcarlton<jwcarl... (AT) gmail (DOT) com> wrote:

You mean - use a regexp??

http://dev.mysql.com/doc/refman/5.1/en/regexp.html

select * from table where col1 REGEXP '%<img(.*?)src="*(\s*)<a%';

HA! I should have known it would be something as simple as that.

I'm getting an error using that, but I'm sure I'll figure it out. The
error is:

'repetition-operator operand invalid' from regexp

Narrowing it down, I'm still getting the same error on this:

SELECT * FROM table WHERE col1 REGEXP 'img(.*?)'

The "repetition-operator" would have to be the *, right? According to
the dev.mysql.com link above, this is valid?

For anyone else reading this, the ? apparently doesn't translate to
"not greedy" in MySQL; I removed it, and the regex worked fine. So,
while REGEXP did work for this particular application, I'm assuming
that it's still greedy, so it doesn't exactly work in all applications.
MySQL uses POSIX style while preg_* PHP functions use Perl style. You'll
also miss stuff like \s (you have to use [[:space:]] instead). Here's
the basic reference:

http://dev.mysql.com/doc/refman/5.1/en/regexp.html

You can probably google for "man 7 regex" and find more information.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

Reply With Quote
  #7  
Old   
Axel Schwenke
 
Posts: n/a

Default Re: Non-greedy wildcard? - 03-25-2011 , 03:59 AM



jwcarlton <jwcarlton (AT) gmail (DOT) com> wrote:
Quote:
I've been trying to find this, but I'm not sure that I'm using the
right terminology. Sorry if this is an FAQ.

I'm trying to SELECT something using a non-greedy wildcard. I know to
use % for a greedy wildcard, but how would I correctly do the
following in MySQL?

%<img(.*?)src="*(\s*)<a%
Not? Because MySQL has no way to return matched substrings?

When it comes to matching as such, then greedyness does not matter.
Each wildcard is as greedy as necessary to make the match.

Example1:

mysql> -- here the first % matches 'cdef'
mysql> select 'abcdefdefg' like 'ab%defg%';
+------------------------------+
Quote:
'abcdefdefg' like 'ab%defg%' |
+------------------------------+
1 |
+------------------------------+

mysql> -- here the first % can match 'c' or 'cdef'
mysql> -- does not matter unless we ask for the matched substrings
mysql> select 'abcdefdefg' like 'ab%def%';
+-----------------------------+
Quote:
'abcdefdefg' like 'ab%def%' |
+-----------------------------+
1 |
+-----------------------------+
1 row in set (0,00 sec)


Example2:

mysql> SELECT 'foo<img src=bar.jpg><a blubb' LIKE '%<img%';
+----------------------------------------------+
Quote:
'foo<img src=bar.jpg><a blubb' LIKE '%<img%' |
+----------------------------------------------+
1 |
+----------------------------------------------+
1 row in set (0,00 sec)

mysql> SELECT 'foo<img src=bar.jpg><a blubb' LIKE '%<img%src=%';
+---------------------------------------------------+
Quote:
'foo<img src=bar.jpg><a blubb' LIKE '%<img%src=%' |
+---------------------------------------------------+
1 |
+---------------------------------------------------+
1 row in set (0,00 sec)

mysql> SELECT 'foo<img src=bar.jpg><a blubb' LIKE '%<img%src=%<a%';

+------------------------------------------------------+
Quote:
'foo<img src=bar.jpg><a blubb' LIKE '%<img%src=%<a%' |
+------------------------------------------------------+
1 |
+------------------------------------------------------+
1 row in set (0,00 sec)


XL

Reply With Quote
Reply




Thread Tools
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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.