Solved

C function that behaves exactly like the SQL LIKE function

Posted on 2004-09-10
7
189 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
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now