Solved

C function that behaves exactly like the SQL LIKE function

Posted on 2004-09-10
7
201 Views
Last Modified: 2010-04-15
Hi,

Does anyone know of a function or can provide an code sample of a C function that behaves exactly like the SQL LIKE function?  The function should accept and properly evaluate all wildcards and special operators used by the SQL LIKE ( %, ^, [], etc ).

Thanks In Advance.
0
Comment
Question by:CUGroup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12033134
Hi CUGroup,

If you can describe the exact behaviour or SQL LIKE and tell us the platform that you are working on, hopefully we can come up with something
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12043087
Hi,

It should be relatively straight forward to convert from SQL syntax to standard Regular Expression syntax (man regex on Unix)

e.g.

_ -> .
% -> .*

etc.
the write wrapper function(s) on the regex library calls, to convert the LIKE string to the regular exp. and to process from that...

Hope this helps,
C.
0
 

Author Comment

by:CUGroup
ID: 12069571
Hi,

Thanks for the responses.  Unfortunately, I need function to be portable, UNIX and Windows so I'm in search of a c function to mimic this sql function.

This is the best resource I could find regarding the use of the SQL LIKE function.

http://www.sql-tutorial.net/SQL-LIKE.asp

Thank you for your help.

0
 
LVL 2

Accepted Solution

by:
sneeuw_chan earned 125 total points
ID: 12070444
I think it corresponds more to 'glob' than to 'regex'.
(Glob is the way that a dos-prompt or unix shell matches filenames on the commandline.)

You could try looking for a 'glob'  library.
A quick google shows this, for example:  http://www.cs.yorku.ca/~oz/glob.bun
(You could even change that code to parse this 'LIKE' format directly, just change the case '*'  to case '%' and the case '?' to case '_'.)

If that's not to your liking, search for other glob libraries.
0
 
LVL 11

Assisted Solution

by:cjjclifford
cjjclifford earned 125 total points
ID: 12072899
CUGroup, if you are just looking for a portable (Unix and Windows) way of matching patterns in text, then surely the standard Posix regex functionality I listed would be the way to go? I'd be confident that there is a Windows inplementation (defintely if you use a GCC derived compiler)
Either that, or as sneeuw_chan suggested use Glob functionality.
I wouldn't go with the SQL LIKE format, unless you are actually implementing a SQL engine... LIKE is a lot more cumbersome than regex or glob...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question